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 /* Eventual name suffix of the trace to set */
230 char *trace_name_suffix
;
232 /* Trace attributes */
233 enum bt_ctf_byte_order trace_bo
;
234 uint64_t trace_major
;
235 uint64_t trace_minor
;
236 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
239 * Stream IDs to stream classes.
241 * int64_t -> struct bt_ctf_stream_class *
243 GHashTable
*stream_classes
;
249 struct ctf_visitor_generate_ir
{ };
252 const char *loglevel_str
[] = {
253 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
254 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
255 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
256 [ LOGLEVEL_ERR
] = "TRACE_ERR",
257 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
258 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
259 [ LOGLEVEL_INFO
] = "TRACE_INFO",
260 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
261 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
262 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
263 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
264 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
265 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
266 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
267 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
271 const char *print_loglevel(int64_t value
)
276 if (value
>= _NR_LOGLEVELS
) {
277 return "<<UNKNOWN>>";
279 return loglevel_str
[value
];
283 * Creates a new declaration scope.
285 * @param par_scope Parent scope (NULL if creating a root scope)
286 * @returns New declaration scope, or NULL on error
289 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
291 struct ctx_decl_scope
*scope
;
293 scope
= g_new(struct ctx_decl_scope
, 1);
298 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
299 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
300 scope
->parent_scope
= par_scope
;
307 * Destroys a declaration scope.
309 * This function does not destroy the parent scope.
311 * @param scope Scope to destroy
314 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
320 g_hash_table_destroy(scope
->decl_map
);
328 * Returns the GQuark of a prefixed alias.
330 * @param prefix Prefix character
332 * @returns Associated GQuark, or 0 on error
335 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
341 /* Prefix character + original string + '\0' */
342 char *prname
= g_new(char, strlen(name
) + 2);
347 sprintf(prname
, "%c%s", prefix
, name
);
348 qname
= g_quark_from_string(prname
);
356 * Looks up a prefixed type alias within a declaration scope.
358 * @param scope Declaration scope
359 * @param prefix Prefix character
360 * @param name Alias name
361 * @param level Number of levels to dig (-1 means infinite)
362 * @returns Declaration, or NULL if not found
365 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
366 struct ctx_decl_scope
*scope
, char prefix
,
367 const char *name
, int levels
)
371 _BT_CTF_FIELD_TYPE_INIT(decl
);
372 struct ctx_decl_scope
*cur_scope
= scope
;
376 qname
= get_prefixed_named_quark(prefix
, name
);
385 while (cur_scope
&& cur_levels
< levels
) {
386 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
387 (gconstpointer
) GUINT_TO_POINTER(qname
));
389 /* Caller's reference */
394 cur_scope
= cur_scope
->parent_scope
;
405 * Looks up a type alias within a declaration scope.
407 * @param scope Declaration scope
408 * @param name Alias name
409 * @param level Number of levels to dig (-1 means infinite)
410 * @returns Declaration, or NULL if not found
413 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
414 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
416 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
421 * Looks up an enumeration within a declaration scope.
423 * @param scope Declaration scope
424 * @param name Enumeration name
425 * @param level Number of levels to dig (-1 means infinite)
426 * @returns Declaration, or NULL if not found
429 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
430 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
432 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
437 * Looks up a structure within a declaration scope.
439 * @param scope Declaration scope
440 * @param name Structure name
441 * @param level Number of levels to dig (-1 means infinite)
442 * @returns Declaration, or NULL if not found
445 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
446 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
448 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
453 * Looks up a variant within a declaration scope.
455 * @param scope Declaration scope
456 * @param name Variant name
457 * @param level Number of levels to dig (-1 means infinite)
458 * @returns Declaration, or NULL if not found
461 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
462 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
464 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
469 * Registers a prefixed type alias within a declaration scope.
471 * @param scope Declaration scope
472 * @param prefix Prefix character
473 * @param name Alias name (non-NULL)
474 * @param decl Declaration to register
475 * @returns 0 if registration went okay, negative value otherwise
478 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
479 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
483 _BT_CTF_FIELD_TYPE_INIT(edecl
);
488 qname
= get_prefixed_named_quark(prefix
, name
);
494 /* Make sure alias does not exist in local scope */
495 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
502 g_hash_table_insert(scope
->decl_map
,
503 GUINT_TO_POINTER(qname
), decl
);
505 /* Hash table's reference */
515 * Registers a type alias within a declaration scope.
517 * @param scope Declaration scope
518 * @param name Alias name (non-NULL)
519 * @param decl Declaration to register
520 * @returns 0 if registration went okay, negative value otherwise
523 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
524 const char *name
, struct bt_ctf_field_type
*decl
)
526 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
531 * Registers an enumeration declaration within a declaration scope.
533 * @param scope Declaration scope
534 * @param name Enumeration name (non-NULL)
535 * @param decl Enumeration declaration to register
536 * @returns 0 if registration went okay, negative value otherwise
539 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
540 const char *name
, struct bt_ctf_field_type
*decl
)
542 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
547 * Registers a structure declaration within a declaration scope.
549 * @param scope Declaration scope
550 * @param name Structure name (non-NULL)
551 * @param decl Structure declaration to register
552 * @returns 0 if registration went okay, negative value otherwise
555 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
556 const char *name
, struct bt_ctf_field_type
*decl
)
558 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
563 * Registers a variant declaration within a declaration scope.
565 * @param scope Declaration scope
566 * @param name Variant name (non-NULL)
567 * @param decl Variant declaration to register
568 * @returns 0 if registration went okay, negative value otherwise
571 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
572 const char *name
, struct bt_ctf_field_type
*decl
)
574 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
579 * Destroys a visitor context.
581 * @param ctx Visitor context to destroy
584 void ctx_destroy(struct ctx
*ctx
)
586 struct ctx_decl_scope
*scope
;
588 * Destroy all scopes, from current one to the root scope.
595 scope
= ctx
->current_scope
;
598 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
600 ctx_decl_scope_destroy(scope
);
601 scope
= parent_scope
;
606 if (ctx
->stream_classes
) {
607 g_hash_table_destroy(ctx
->stream_classes
);
610 free(ctx
->trace_name_suffix
);
618 * Creates a new visitor context.
620 * @param trace Associated trace
621 * @param efd Error stream
622 * @returns New visitor context, or NULL on error
625 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
,
626 uint64_t clock_class_offset_ns
, const char *trace_name_suffix
)
628 struct ctx
*ctx
= NULL
;
629 struct ctx_decl_scope
*scope
= NULL
;
631 ctx
= g_new0(struct ctx
, 1);
636 /* Root declaration scope */
637 scope
= ctx_decl_scope_create(NULL
);
642 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
643 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
644 if (!ctx
->stream_classes
) {
648 if (trace_name_suffix
) {
649 ctx
->trace_name_suffix
= strdup(trace_name_suffix
);
650 if (!ctx
->trace_name_suffix
) {
657 ctx
->current_scope
= scope
;
659 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
660 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
665 ctx_decl_scope_destroy(scope
);
670 * Pushes a new declaration scope on top of a visitor context's
671 * declaration scope stack.
673 * @param ctx Visitor context
674 * @returns 0 on success, or a negative value on error
677 int ctx_push_scope(struct ctx
*ctx
)
680 struct ctx_decl_scope
*new_scope
;
683 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
689 ctx
->current_scope
= new_scope
;
696 void ctx_pop_scope(struct ctx
*ctx
)
698 struct ctx_decl_scope
*parent_scope
= NULL
;
702 if (!ctx
->current_scope
) {
706 parent_scope
= ctx
->current_scope
->parent_scope
;
707 ctx_decl_scope_destroy(ctx
->current_scope
);
708 ctx
->current_scope
= parent_scope
;
715 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
716 struct bt_ctf_field_type
**decl
);
719 int is_unary_string(struct bt_list_head
*head
)
722 struct ctf_node
*node
;
724 bt_list_for_each_entry(node
, head
, siblings
) {
725 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
729 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
738 char *concatenate_unary_strings(struct bt_list_head
*head
)
742 struct ctf_node
*node
;
744 str
= g_string_new(NULL
);
746 bt_list_for_each_entry(node
, head
, siblings
) {
750 node
->type
!= NODE_UNARY_EXPRESSION
||
751 node
->u
.unary_expression
.type
!= UNARY_STRING
||
754 node
->u
.unary_expression
.link
!=
762 switch (node
->u
.unary_expression
.link
) {
764 g_string_append(str
, ".");
766 case UNARY_ARROWLINK
:
767 g_string_append(str
, "->");
769 case UNARY_DOTDOTDOT
:
770 g_string_append(str
, "...");
776 src_string
= node
->u
.unary_expression
.u
.string
;
777 g_string_append(str
, src_string
);
781 /* Destroys the container, returns the underlying string */
782 return g_string_free(str
, FALSE
);
785 /* This always returns NULL */
786 return g_string_free(str
, TRUE
);
790 const char *get_map_clock_name_value(struct bt_list_head
*head
)
793 struct ctf_node
*node
;
794 const char *name
= NULL
;
796 bt_list_for_each_entry(node
, head
, siblings
) {
798 int uexpr_type
= node
->u
.unary_expression
.type
;
799 int uexpr_link
= node
->u
.unary_expression
.link
;
800 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
801 uexpr_type
!= UNARY_STRING
||
802 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
807 /* Needs to be chained with . */
808 switch (node
->u
.unary_expression
.link
) {
811 case UNARY_ARROWLINK
:
812 case UNARY_DOTDOTDOT
:
818 src_string
= node
->u
.unary_expression
.u
.string
;
822 if (strcmp("clock", src_string
)) {
830 if (strcmp("value", src_string
)) {
835 /* Extra identifier, unknown */
849 int is_unary_unsigned(struct bt_list_head
*head
)
852 struct ctf_node
*node
;
854 bt_list_for_each_entry(node
, head
, siblings
) {
855 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
859 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
868 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
872 struct ctf_node
*node
;
874 if (bt_list_empty(head
)) {
879 bt_list_for_each_entry(node
, head
, siblings
) {
880 int uexpr_type
= node
->u
.unary_expression
.type
;
881 int uexpr_link
= node
->u
.unary_expression
.link
;
882 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
883 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
884 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
890 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
899 int is_unary_signed(struct bt_list_head
*head
)
902 struct ctf_node
*node
;
904 bt_list_for_each_entry(node
, head
, siblings
) {
905 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
909 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
918 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
922 struct ctf_node
*node
;
924 bt_list_for_each_entry(node
, head
, siblings
) {
925 int uexpr_type
= node
->u
.unary_expression
.type
;
926 int uexpr_link
= node
->u
.unary_expression
.link
;
927 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
928 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
929 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
930 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
931 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
937 switch (node
->u
.unary_expression
.type
) {
938 case UNARY_UNSIGNED_CONSTANT
:
940 node
->u
.unary_expression
.u
.unsigned_constant
;
942 case UNARY_SIGNED_CONSTANT
:
943 *value
= node
->u
.unary_expression
.u
.signed_constant
;
958 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
962 struct ctf_node
*node
;
964 bt_list_for_each_entry(node
, head
, siblings
) {
965 int uexpr_type
= node
->u
.unary_expression
.type
;
966 int uexpr_link
= node
->u
.unary_expression
.link
;
967 const char *src_string
;
969 if (node
->type
!= NODE_UNARY_EXPRESSION
||
970 uexpr_type
!= UNARY_STRING
||
971 uexpr_link
!= UNARY_LINK_UNKNOWN
||
977 src_string
= node
->u
.unary_expression
.u
.string
;
978 ret
= bt_uuid_parse(src_string
, uuid
);
989 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
993 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
994 _FPERROR(efd
, "%s", "expecting unary expression");
999 switch (unary_expr
->u
.unary_expression
.type
) {
1000 case UNARY_UNSIGNED_CONSTANT
:
1001 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
1003 case UNARY_SIGNED_CONSTANT
:
1004 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
1008 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
1010 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
1012 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
1015 _FPERROR(efd
, "unexpected string \"%s\"", str
);
1022 _FPERROR(efd
, "%s", "unexpected unary expression type");
1032 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
1033 struct ctf_node
*unary_expr
)
1036 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1038 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1040 "\"byte_order\" attribute: expecting string");
1044 str
= unary_expr
->u
.unary_expression
.u
.string
;
1046 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1047 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1048 } else if (!strcmp(str
, "le")) {
1049 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1050 } else if (!strcmp(str
, "native")) {
1051 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1053 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1063 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1064 struct ctf_node
*uexpr
)
1066 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
1068 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1069 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1076 int is_align_valid(uint64_t align
)
1078 return (align
!= 0) && !(align
& (align
- 1));
1082 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1087 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1092 switch (type_specifier
->u
.type_specifier
.type
) {
1094 g_string_append(str
, "void");
1097 g_string_append(str
, "char");
1099 case TYPESPEC_SHORT
:
1100 g_string_append(str
, "short");
1103 g_string_append(str
, "int");
1106 g_string_append(str
, "long");
1108 case TYPESPEC_FLOAT
:
1109 g_string_append(str
, "float");
1111 case TYPESPEC_DOUBLE
:
1112 g_string_append(str
, "double");
1114 case TYPESPEC_SIGNED
:
1115 g_string_append(str
, "signed");
1117 case TYPESPEC_UNSIGNED
:
1118 g_string_append(str
, "unsigned");
1121 g_string_append(str
, "bool");
1123 case TYPESPEC_COMPLEX
:
1124 g_string_append(str
, "_Complex");
1126 case TYPESPEC_IMAGINARY
:
1127 g_string_append(str
, "_Imaginary");
1129 case TYPESPEC_CONST
:
1130 g_string_append(str
, "const");
1132 case TYPESPEC_ID_TYPE
:
1133 if (type_specifier
->u
.type_specifier
.id_type
) {
1134 g_string_append(str
,
1135 type_specifier
->u
.type_specifier
.id_type
);
1138 case TYPESPEC_STRUCT
:
1140 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1142 if (!node
->u
._struct
.name
) {
1143 _PERROR("%s", "unexpected empty structure name");
1148 g_string_append(str
, "struct ");
1149 g_string_append(str
, node
->u
._struct
.name
);
1152 case TYPESPEC_VARIANT
:
1154 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1156 if (!node
->u
.variant
.name
) {
1157 _PERROR("%s", "unexpected empty variant name");
1162 g_string_append(str
, "variant ");
1163 g_string_append(str
, node
->u
.variant
.name
);
1168 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1170 if (!node
->u
._enum
.enum_id
) {
1171 _PERROR("%s", "unexpected empty enum name");
1176 g_string_append(str
, "enum ");
1177 g_string_append(str
, node
->u
._enum
.enum_id
);
1180 case TYPESPEC_FLOATING_POINT
:
1181 case TYPESPEC_INTEGER
:
1182 case TYPESPEC_STRING
:
1184 _PERROR("%s", "unknown specifier");
1194 int get_type_specifier_list_name(struct ctx
*ctx
,
1195 struct ctf_node
*type_specifier_list
, GString
*str
)
1198 struct ctf_node
*iter
;
1199 int alias_item_nr
= 0;
1200 struct bt_list_head
*head
=
1201 &type_specifier_list
->u
.type_specifier_list
.head
;
1203 bt_list_for_each_entry(iter
, head
, siblings
) {
1204 if (alias_item_nr
!= 0) {
1205 g_string_append(str
, " ");
1209 ret
= get_type_specifier_name(ctx
, iter
, str
);
1220 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1221 struct ctf_node
*type_specifier_list
,
1222 struct ctf_node
*node_type_declarator
)
1228 struct ctf_node
*iter
;
1229 struct bt_list_head
*pointers
=
1230 &node_type_declarator
->u
.type_declarator
.pointers
;
1232 str
= g_string_new("");
1233 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1235 g_string_free(str
, TRUE
);
1239 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1240 g_string_append(str
, " *");
1242 if (iter
->u
.pointer
.const_qualifier
) {
1243 g_string_append(str
, " const");
1247 str_c
= g_string_free(str
, FALSE
);
1248 qalias
= g_quark_from_string(str_c
);
1256 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1257 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1258 struct bt_ctf_field_type
**field_decl
,
1259 struct bt_ctf_field_type
*nested_decl
)
1262 * During this whole function, nested_decl is always OURS,
1263 * whereas field_decl is an output which we create, but
1264 * belongs to the caller (it is moved).
1270 /* Validate type declarator node */
1271 if (node_type_declarator
) {
1272 if (node_type_declarator
->u
.type_declarator
.type
==
1278 /* TODO: GCC bitfields not supported yet */
1279 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1281 _PERROR("%s", "GCC bitfields are not supported as of this version");
1287 /* Find the right nested declaration if not provided */
1289 struct bt_list_head
*pointers
=
1290 &node_type_declarator
->u
.type_declarator
.pointers
;
1292 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1294 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1297 * If we have a pointer declarator, it HAS to
1298 * be present in the typealiases (else fail).
1300 qalias
= create_typealias_identifier(ctx
,
1301 type_specifier_list
, node_type_declarator
);
1303 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1304 g_quark_to_string(qalias
), -1);
1306 _PERROR("cannot find typealias \"%s\"",
1307 g_quark_to_string(qalias
));
1312 /* Make a copy of it */
1313 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1314 BT_PUT(nested_decl
);
1315 if (!nested_decl_copy
) {
1316 _PERROR("%s", "cannot copy nested field type");
1321 BT_MOVE(nested_decl
, nested_decl_copy
);
1323 /* Force integer's base to 16 since it's a pointer */
1324 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1325 bt_ctf_field_type_integer_set_base(nested_decl
,
1326 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1329 ret
= visit_type_specifier_list(ctx
,
1330 type_specifier_list
, &nested_decl
);
1332 assert(!nested_decl
);
1338 assert(nested_decl
);
1340 if (!node_type_declarator
) {
1341 BT_MOVE(*field_decl
, nested_decl
);
1345 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1346 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1348 node_type_declarator
->u
.type_declarator
.u
.id
;
1350 *field_name
= g_quark_from_string(id
);
1355 BT_MOVE(*field_decl
, nested_decl
);
1358 struct ctf_node
*first
;
1359 _BT_CTF_FIELD_TYPE_INIT(decl
);
1360 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1361 struct bt_list_head
*length
=
1362 &node_type_declarator
->
1363 u
.type_declarator
.u
.nested
.length
;
1365 /* Create array/sequence, pass nested_decl as child */
1366 if (bt_list_empty(length
)) {
1368 "expecting length field reference or value");
1373 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1374 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1379 switch (first
->u
.unary_expression
.type
) {
1380 case UNARY_UNSIGNED_CONSTANT
:
1383 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1385 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1386 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1388 BT_PUT(nested_decl
);
1391 "cannot create array field type");
1396 BT_MOVE(decl
, array_decl
);
1401 /* Lookup unsigned integer definition, create seq. */
1402 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1403 char *length_name
= concatenate_unary_strings(length
);
1410 seq_decl
= bt_ctf_field_type_sequence_create(
1411 nested_decl
, length_name
);
1412 g_free(length_name
);
1413 BT_PUT(nested_decl
);
1416 "cannot create sequence field type");
1421 BT_MOVE(decl
, seq_decl
);
1429 assert(!nested_decl
);
1431 assert(!*field_decl
);
1434 * At this point, we found the next nested declaration.
1435 * We currently own this (and lost the ownership of
1436 * nested_decl in the meantime). Pass this next
1437 * nested declaration as the content of the outer
1438 * container, MOVING its ownership.
1440 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1442 node_type_declarator
->
1443 u
.type_declarator
.u
.nested
.type_declarator
,
1444 &outer_field_decl
, decl
);
1447 assert(!outer_field_decl
);
1452 assert(outer_field_decl
);
1453 BT_MOVE(*field_decl
, outer_field_decl
);
1457 BT_PUT(nested_decl
);
1458 assert(*field_decl
);
1463 BT_PUT(nested_decl
);
1464 BT_PUT(*field_decl
);
1470 int visit_struct_decl_field(struct ctx
*ctx
,
1471 struct bt_ctf_field_type
*struct_decl
,
1472 struct ctf_node
*type_specifier_list
,
1473 struct bt_list_head
*type_declarators
)
1476 struct ctf_node
*iter
;
1477 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1479 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1482 const char *field_name
;
1483 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1485 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1486 &qfield_name
, iter
, &field_decl
, NULL
);
1488 assert(!field_decl
);
1489 _PERROR("%s", "cannot visit type declarator");
1494 field_name
= g_quark_to_string(qfield_name
);
1496 /* Check if field with same name already exists */
1498 bt_ctf_field_type_structure_get_field_type_by_name(
1499 struct_decl
, field_name
);
1501 BT_PUT(efield_decl
);
1502 _PERROR("duplicate field \"%s\" in structure",
1508 /* Add field to structure */
1509 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1510 field_decl
, field_name
);
1513 _PERROR("cannot add field \"%s\" to structure",
1514 g_quark_to_string(qfield_name
));
1528 int visit_variant_decl_field(struct ctx
*ctx
,
1529 struct bt_ctf_field_type
*variant_decl
,
1530 struct ctf_node
*type_specifier_list
,
1531 struct bt_list_head
*type_declarators
)
1534 struct ctf_node
*iter
;
1535 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1537 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1540 const char *field_name
;
1541 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1543 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1544 &qfield_name
, iter
, &field_decl
, NULL
);
1546 assert(!field_decl
);
1548 "cannot visit type declarator");
1553 field_name
= g_quark_to_string(qfield_name
);
1555 /* Check if field with same name already exists */
1557 bt_ctf_field_type_variant_get_field_type_by_name(
1558 variant_decl
, field_name
);
1560 BT_PUT(efield_decl
);
1561 _PERROR("duplicate field \"%s\" in variant",
1567 /* Add field to structure */
1568 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1569 field_decl
, field_name
);
1572 _PERROR("cannot add field \"%s\" to variant",
1573 g_quark_to_string(qfield_name
));
1587 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1588 struct bt_list_head
*type_declarators
)
1592 struct ctf_node
*iter
;
1593 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1595 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1596 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1597 &qidentifier
, iter
, &type_decl
, NULL
);
1599 _PERROR("%s", "cannot visit type declarator");
1604 /* Do not allow typedef and typealias of untagged variants */
1605 if (bt_ctf_field_type_is_variant(type_decl
)) {
1606 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1607 _PERROR("%s", "typedef of untagged variant is not allowed");
1613 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1614 g_quark_to_string(qidentifier
), type_decl
);
1616 _PERROR("cannot register typedef \"%s\"",
1617 g_quark_to_string(qidentifier
));
1629 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1630 struct ctf_node
*alias
)
1634 struct ctf_node
*node
;
1635 GQuark qdummy_field_name
;
1636 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1638 /* Create target type declaration */
1639 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1642 node
= _BT_LIST_FIRST_ENTRY(
1643 &target
->u
.typealias_target
.type_declarators
,
1644 struct ctf_node
, siblings
);
1647 ret
= visit_type_declarator(ctx
,
1648 target
->u
.typealias_target
.type_specifier_list
,
1649 &qdummy_field_name
, node
, &type_decl
, NULL
);
1652 _PERROR("%s", "cannot visit type declarator");
1656 /* Do not allow typedef and typealias of untagged variants */
1657 if (bt_ctf_field_type_is_variant(type_decl
)) {
1658 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1660 "typealias of untagged variant is not allowed");
1667 * The semantic validator does not check whether the target is
1668 * abstract or not (if it has an identifier). Check it here.
1670 if (qdummy_field_name
!= 0) {
1671 _PERROR("%s", "expecting empty identifier");
1676 /* Create alias identifier */
1677 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1678 struct ctf_node
, siblings
);
1679 qalias
= create_typealias_identifier(ctx
,
1680 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1681 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1682 g_quark_to_string(qalias
), type_decl
);
1684 _PERROR("cannot register typealias \"%s\"",
1685 g_quark_to_string(qalias
));
1696 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1697 struct bt_ctf_field_type
*struct_decl
)
1701 switch (entry_node
->type
) {
1703 ret
= visit_typedef(ctx
,
1704 entry_node
->u
._typedef
.type_specifier_list
,
1705 &entry_node
->u
._typedef
.type_declarators
);
1708 "cannot add typedef in \"struct\" declaration");
1712 case NODE_TYPEALIAS
:
1713 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1714 entry_node
->u
.typealias
.alias
);
1717 "cannot add typealias in \"struct\" declaration");
1721 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1723 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1724 entry_node
->u
.struct_or_variant_declaration
.
1725 type_specifier_list
,
1726 &entry_node
->u
.struct_or_variant_declaration
.
1733 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1743 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1744 struct bt_ctf_field_type
*variant_decl
)
1748 switch (entry_node
->type
) {
1750 ret
= visit_typedef(ctx
,
1751 entry_node
->u
._typedef
.type_specifier_list
,
1752 &entry_node
->u
._typedef
.type_declarators
);
1755 "cannot add typedef in \"variant\" declaration");
1759 case NODE_TYPEALIAS
:
1760 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1761 entry_node
->u
.typealias
.alias
);
1764 "cannot add typealias in \"variant\" declaration");
1768 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1770 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1771 entry_node
->u
.struct_or_variant_declaration
.
1772 type_specifier_list
,
1773 &entry_node
->u
.struct_or_variant_declaration
.
1780 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1790 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1791 struct bt_list_head
*decl_list
, int has_body
,
1792 struct bt_list_head
*min_align
,
1793 struct bt_ctf_field_type
**struct_decl
)
1797 *struct_decl
= NULL
;
1799 /* For named struct (without body), lookup in declaration scope */
1801 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1808 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1810 if (!*struct_decl
) {
1811 _PERROR("cannot find \"struct %s\"", name
);
1816 /* Make a copy of it */
1817 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1818 if (!struct_decl_copy
) {
1820 "cannot create copy of structure field type");
1825 BT_MOVE(*struct_decl
, struct_decl_copy
);
1827 struct ctf_node
*entry_node
;
1828 uint64_t min_align_value
= 0;
1831 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1833 estruct_decl
= ctx_decl_scope_lookup_struct(
1834 ctx
->current_scope
, name
, 1);
1836 BT_PUT(estruct_decl
);
1837 _PERROR("\"struct %s\" already declared in local scope",
1844 if (!bt_list_empty(min_align
)) {
1845 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1847 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1852 *struct_decl
= bt_ctf_field_type_structure_create();
1853 if (!*struct_decl
) {
1854 _PERROR("%s", "cannot create structure field type");
1859 if (min_align_value
!= 0) {
1860 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1863 _PERROR("%s", "failed to set structure's minimal alignment");
1868 ret
= ctx_push_scope(ctx
);
1870 _PERROR("%s", "cannot push scope");
1874 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1875 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1886 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1887 name
, *struct_decl
);
1889 _PERROR("cannot register \"struct %s\" in declaration scope",
1899 BT_PUT(*struct_decl
);
1905 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1906 const char *tag
, struct bt_list_head
*decl_list
,
1907 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1910 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1912 *variant_decl
= NULL
;
1914 /* For named variant (without body), lookup in declaration scope */
1916 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1923 untagged_variant_decl
=
1924 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1926 if (!untagged_variant_decl
) {
1927 _PERROR("cannot find \"variant %s\"", name
);
1932 /* Make a copy of it */
1933 variant_decl_copy
= bt_ctf_field_type_copy(
1934 untagged_variant_decl
);
1935 if (!variant_decl_copy
) {
1937 "cannot create copy of variant field type");
1942 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1944 struct ctf_node
*entry_node
;
1947 struct bt_ctf_field_type
*evariant_decl
=
1948 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1951 if (evariant_decl
) {
1952 BT_PUT(evariant_decl
);
1953 _PERROR("\"variant %s\" already declared in local scope",
1960 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1962 if (!untagged_variant_decl
) {
1963 _PERROR("%s", "cannot create variant field type");
1968 ret
= ctx_push_scope(ctx
);
1970 _PERROR("%s", "cannot push scope");
1974 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1975 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1976 untagged_variant_decl
);
1986 ret
= ctx_decl_scope_register_variant(
1987 ctx
->current_scope
, name
,
1988 untagged_variant_decl
);
1990 _PERROR("cannot register \"variant %s\" in declaration scope",
1998 * If tagged, create tagged variant and return; otherwise
1999 * return untagged variant.
2002 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2005 * At this point, we have a fresh untagged variant; nobody
2006 * else owns it. Set its tag now.
2008 ret
= bt_ctf_field_type_variant_set_tag_name(
2009 untagged_variant_decl
, tag
);
2014 BT_MOVE(*variant_decl
, untagged_variant_decl
);
2017 assert(!untagged_variant_decl
);
2018 assert(*variant_decl
);
2023 BT_PUT(untagged_variant_decl
);
2024 BT_PUT(*variant_decl
);
2030 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2031 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2035 struct ctf_node
*iter
;
2036 int64_t start
= 0, end
= 0;
2037 const char *label
= enumerator
->u
.enumerator
.id
;
2038 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2040 bt_list_for_each_entry(iter
, values
, siblings
) {
2043 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2044 _PERROR("wrong unary expression for enumeration label \"%s\"",
2056 switch (iter
->u
.unary_expression
.type
) {
2057 case UNARY_SIGNED_CONSTANT
:
2058 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2060 case UNARY_UNSIGNED_CONSTANT
:
2062 iter
->u
.unary_expression
.u
.unsigned_constant
;
2065 _PERROR("invalid enumeration entry: \"%s\"",
2072 _PERROR("invalid enumeration entry: \"%s\"",
2092 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2095 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2096 label
, (uint64_t) start
, (uint64_t) end
);
2099 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2111 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2112 struct ctf_node
*container_type
,
2113 struct bt_list_head
*enumerator_list
,
2115 struct bt_ctf_field_type
**enum_decl
)
2119 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2123 /* For named enum (without body), lookup in declaration scope */
2125 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2132 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2135 _PERROR("cannot find \"enum %s\"", name
);
2140 /* Make a copy of it */
2141 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2142 if (!enum_decl_copy
) {
2144 "cannot create copy of enumeration field type");
2150 BT_MOVE(*enum_decl
, enum_decl_copy
);
2152 struct ctf_node
*iter
;
2153 int64_t last_value
= 0;
2156 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2158 eenum_decl
= ctx_decl_scope_lookup_enum(
2159 ctx
->current_scope
, name
, 1);
2162 _PERROR("\"enum %s\" already declared in local scope",
2169 if (!container_type
) {
2170 integer_decl
= ctx_decl_scope_lookup_alias(
2171 ctx
->current_scope
, "int", -1);
2172 if (!integer_decl
) {
2173 _PERROR("%s", "cannot find \"int\" type for enumeration");
2178 ret
= visit_type_declarator(ctx
, container_type
,
2179 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2181 assert(!integer_decl
);
2187 assert(integer_decl
);
2189 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2190 _PERROR("%s", "container type for enumeration is not an integer");
2195 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2197 _PERROR("%s", "cannot create enumeration field type");
2202 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2203 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2205 bt_ctf_field_type_integer_get_signed(integer_decl
));
2212 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2220 BT_PUT(integer_decl
);
2225 BT_PUT(integer_decl
);
2232 int visit_type_specifier(struct ctx
*ctx
,
2233 struct ctf_node
*type_specifier_list
,
2234 struct bt_ctf_field_type
**decl
)
2237 GString
*str
= NULL
;
2238 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2241 str
= g_string_new("");
2242 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2247 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2249 _PERROR("cannot find type alias \"%s\"", str
->str
);
2254 /* Make a copy of the type declaration */
2255 decl_copy
= bt_ctf_field_type_copy(*decl
);
2257 _PERROR("%s", "cannot create field type copy");
2262 BT_MOVE(*decl
, decl_copy
);
2263 (void) g_string_free(str
, TRUE
);
2270 (void) g_string_free(str
, TRUE
);
2279 int visit_integer_decl(struct ctx
*ctx
,
2280 struct bt_list_head
*expressions
,
2281 struct bt_ctf_field_type
**integer_decl
)
2286 struct ctf_node
*expression
;
2287 uint64_t alignment
= 0, size
= 0;
2288 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2289 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2290 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2291 enum bt_ctf_byte_order byte_order
=
2292 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2294 *integer_decl
= NULL
;
2296 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2297 struct ctf_node
*left
, *right
;
2299 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2300 struct ctf_node
, siblings
);
2301 right
= _BT_LIST_FIRST_ENTRY(
2302 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2305 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2310 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2311 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2312 _PERROR_DUP_ATTR("signed",
2313 "integer declaration");
2318 signedness
= get_boolean(ctx
->efd
, right
);
2319 if (signedness
< 0) {
2324 _SET(&set
, _INTEGER_SIGNED_SET
);
2325 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2327 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2328 _PERROR_DUP_ATTR("byte_order",
2329 "integer declaration");
2334 byte_order
= get_real_byte_order(ctx
, right
);
2335 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2336 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2341 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2342 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2343 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2344 _PERROR_DUP_ATTR("size",
2345 "integer declaration");
2350 if (right
->u
.unary_expression
.type
!=
2351 UNARY_UNSIGNED_CONSTANT
) {
2352 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2357 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2359 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2362 } else if (size
> 64) {
2363 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2368 _SET(&set
, _INTEGER_SIZE_SET
);
2369 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2371 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2372 _PERROR_DUP_ATTR("align",
2373 "integer declaration");
2378 if (right
->u
.unary_expression
.type
!=
2379 UNARY_UNSIGNED_CONSTANT
) {
2380 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2386 right
->u
.unary_expression
.u
.unsigned_constant
;
2387 if (!is_align_valid(alignment
)) {
2388 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2393 _SET(&set
, _INTEGER_ALIGN_SET
);
2394 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2395 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2396 _PERROR_DUP_ATTR("base", "integer declaration");
2401 switch (right
->u
.unary_expression
.type
) {
2402 case UNARY_UNSIGNED_CONSTANT
:
2404 uint64_t constant
= right
->u
.unary_expression
.
2405 u
.unsigned_constant
;
2409 base
= BT_CTF_INTEGER_BASE_BINARY
;
2412 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2415 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2418 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2421 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2422 right
->u
.unary_expression
.u
.unsigned_constant
);
2430 char *s_right
= concatenate_unary_strings(
2431 &expression
->u
.ctf_expression
.right
);
2433 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2438 if (!strcmp(s_right
, "decimal") ||
2439 !strcmp(s_right
, "dec") ||
2440 !strcmp(s_right
, "d") ||
2441 !strcmp(s_right
, "i") ||
2442 !strcmp(s_right
, "u")) {
2443 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2444 } else if (!strcmp(s_right
, "hexadecimal") ||
2445 !strcmp(s_right
, "hex") ||
2446 !strcmp(s_right
, "x") ||
2447 !strcmp(s_right
, "X") ||
2448 !strcmp(s_right
, "p")) {
2449 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2450 } else if (!strcmp(s_right
, "octal") ||
2451 !strcmp(s_right
, "oct") ||
2452 !strcmp(s_right
, "o")) {
2453 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2454 } else if (!strcmp(s_right
, "binary") ||
2455 !strcmp(s_right
, "b")) {
2456 base
= BT_CTF_INTEGER_BASE_BINARY
;
2458 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2469 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2474 _SET(&set
, _INTEGER_BASE_SET
);
2475 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2479 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2480 _PERROR_DUP_ATTR("encoding",
2481 "integer declaration");
2486 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2487 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2492 s_right
= concatenate_unary_strings(
2493 &expression
->u
.ctf_expression
.right
);
2495 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2500 if (!strcmp(s_right
, "UTF8") ||
2501 !strcmp(s_right
, "utf8") ||
2502 !strcmp(s_right
, "utf-8") ||
2503 !strcmp(s_right
, "UTF-8")) {
2504 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2505 } else if (!strcmp(s_right
, "ASCII") ||
2506 !strcmp(s_right
, "ascii")) {
2507 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2508 } else if (!strcmp(s_right
, "none")) {
2509 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2511 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2519 _SET(&set
, _INTEGER_ENCODING_SET
);
2520 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2521 const char *clock_name
;
2523 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2524 _PERROR_DUP_ATTR("map", "integer declaration");
2529 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2530 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2536 get_map_clock_name_value(
2537 &expression
->u
.ctf_expression
.right
);
2539 char *s_right
= concatenate_unary_strings(
2540 &expression
->u
.ctf_expression
.right
);
2543 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2548 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"",
2550 _SET(&set
, _INTEGER_MAP_SET
);
2555 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2556 ctx
->trace
, clock_name
);
2557 if (!mapped_clock
) {
2558 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"",
2564 _SET(&set
, _INTEGER_MAP_SET
);
2566 _PWARNING("unknown attribute \"%s\" in integer declaration",
2567 left
->u
.unary_expression
.u
.string
);
2571 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2573 "missing \"size\" attribute in integer declaration");
2578 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2579 if (size
% CHAR_BIT
) {
2580 /* Bit-packed alignment */
2583 /* Byte-packed alignment */
2584 alignment
= CHAR_BIT
;
2588 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2589 if (!*integer_decl
) {
2590 _PERROR("%s", "cannot create integer field type");
2595 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2596 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2597 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2598 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2599 (unsigned int) alignment
);
2600 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2604 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2605 *integer_decl
, mapped_clock
);
2606 bt_put(mapped_clock
);
2607 mapped_clock
= NULL
;
2611 _PERROR("%s", "cannot configure integer field type");
2620 bt_put(mapped_clock
);
2623 BT_PUT(*integer_decl
);
2629 int visit_floating_point_number_decl(struct ctx
*ctx
,
2630 struct bt_list_head
*expressions
,
2631 struct bt_ctf_field_type
**float_decl
)
2635 struct ctf_node
*expression
;
2636 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2637 enum bt_ctf_byte_order byte_order
=
2638 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2642 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2643 struct ctf_node
*left
, *right
;
2645 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2646 struct ctf_node
, siblings
);
2647 right
= _BT_LIST_FIRST_ENTRY(
2648 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2651 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2656 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2657 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2658 _PERROR_DUP_ATTR("byte_order",
2659 "floating point number declaration");
2664 byte_order
= get_real_byte_order(ctx
, right
);
2665 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2666 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2671 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2672 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2674 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2675 _PERROR_DUP_ATTR("exp_dig",
2676 "floating point number declaration");
2681 if (right
->u
.unary_expression
.type
!=
2682 UNARY_UNSIGNED_CONSTANT
) {
2683 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2688 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2689 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2690 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2692 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2693 _PERROR_DUP_ATTR("mant_dig",
2694 "floating point number declaration");
2699 if (right
->u
.unary_expression
.type
!=
2700 UNARY_UNSIGNED_CONSTANT
) {
2701 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2706 mant_dig
= right
->u
.unary_expression
.u
.
2708 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2709 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2711 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2712 _PERROR_DUP_ATTR("align",
2713 "floating point number declaration");
2718 if (right
->u
.unary_expression
.type
!=
2719 UNARY_UNSIGNED_CONSTANT
) {
2720 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2725 alignment
= right
->u
.unary_expression
.u
.
2728 if (!is_align_valid(alignment
)) {
2729 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2734 _SET(&set
, _FLOAT_ALIGN_SET
);
2736 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2737 left
->u
.unary_expression
.u
.string
);
2741 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2742 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2747 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2748 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2753 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2754 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2755 /* Bit-packed alignment */
2758 /* Byte-packed alignment */
2759 alignment
= CHAR_BIT
;
2763 *float_decl
= bt_ctf_field_type_floating_point_create();
2766 "cannot create floating point number field type");
2771 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2772 *float_decl
, exp_dig
);
2773 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2774 *float_decl
, mant_dig
);
2775 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2776 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2779 "cannot configure floating point number field type");
2787 BT_PUT(*float_decl
);
2793 int visit_string_decl(struct ctx
*ctx
,
2794 struct bt_list_head
*expressions
,
2795 struct bt_ctf_field_type
**string_decl
)
2799 struct ctf_node
*expression
;
2800 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2802 *string_decl
= NULL
;
2804 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2805 struct ctf_node
*left
, *right
;
2807 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2808 struct ctf_node
, siblings
);
2809 right
= _BT_LIST_FIRST_ENTRY(
2810 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2813 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2818 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2821 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2822 _PERROR_DUP_ATTR("encoding",
2823 "string declaration");
2828 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2829 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2834 s_right
= concatenate_unary_strings(
2835 &expression
->u
.ctf_expression
.right
);
2837 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2842 if (!strcmp(s_right
, "UTF8") ||
2843 !strcmp(s_right
, "utf8") ||
2844 !strcmp(s_right
, "utf-8") ||
2845 !strcmp(s_right
, "UTF-8")) {
2846 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2847 } else if (!strcmp(s_right
, "ASCII") ||
2848 !strcmp(s_right
, "ascii")) {
2849 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2850 } else if (!strcmp(s_right
, "none")) {
2851 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2853 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2861 _SET(&set
, _STRING_ENCODING_SET
);
2863 _PWARNING("unknown attribute \"%s\" in string declaration",
2864 left
->u
.unary_expression
.u
.string
);
2868 *string_decl
= bt_ctf_field_type_string_create();
2869 if (!*string_decl
) {
2870 _PERROR("%s", "cannot create string field type");
2875 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2877 _PERROR("%s", "cannot configure string field type");
2885 BT_PUT(*string_decl
);
2891 int visit_type_specifier_list(struct ctx
*ctx
,
2892 struct ctf_node
*ts_list
,
2893 struct bt_ctf_field_type
**decl
)
2896 struct ctf_node
*first
, *node
;
2900 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2905 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2906 struct ctf_node
, siblings
);
2907 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2912 node
= first
->u
.type_specifier
.node
;
2914 switch (first
->u
.type_specifier
.type
) {
2915 case TYPESPEC_INTEGER
:
2916 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2923 case TYPESPEC_FLOATING_POINT
:
2924 ret
= visit_floating_point_number_decl(ctx
,
2925 &node
->u
.floating_point
.expressions
, decl
);
2931 case TYPESPEC_STRING
:
2932 ret
= visit_string_decl(ctx
,
2933 &node
->u
.string
.expressions
, decl
);
2939 case TYPESPEC_STRUCT
:
2940 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2941 &node
->u
._struct
.declaration_list
,
2942 node
->u
._struct
.has_body
,
2943 &node
->u
._struct
.min_align
, decl
);
2949 case TYPESPEC_VARIANT
:
2950 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2951 node
->u
.variant
.choice
,
2952 &node
->u
.variant
.declaration_list
,
2953 node
->u
.variant
.has_body
, decl
);
2960 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2961 node
->u
._enum
.container_type
,
2962 &node
->u
._enum
.enumerator_list
,
2963 node
->u
._enum
.has_body
, decl
);
2971 case TYPESPEC_SHORT
:
2974 case TYPESPEC_FLOAT
:
2975 case TYPESPEC_DOUBLE
:
2976 case TYPESPEC_SIGNED
:
2977 case TYPESPEC_UNSIGNED
:
2979 case TYPESPEC_COMPLEX
:
2980 case TYPESPEC_IMAGINARY
:
2981 case TYPESPEC_CONST
:
2982 case TYPESPEC_ID_TYPE
:
2983 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2990 _PERROR("unexpected node type: %d",
2991 (int) first
->u
.type_specifier
.type
);
3007 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3008 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
3013 _BT_CTF_FIELD_TYPE_INIT(decl
);
3015 switch (node
->type
) {
3017 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3018 &node
->u
._typedef
.type_declarators
);
3021 "cannot add typedef in \"event\" declaration");
3025 case NODE_TYPEALIAS
:
3026 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3027 node
->u
.typealias
.alias
);
3029 _PERROR("%s", "cannot add typealias in \"event\" declaration");
3033 case NODE_CTF_EXPRESSION
:
3035 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3041 if (!strcmp(left
, "name")) {
3042 /* This is already known at this stage */
3043 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3044 _PERROR_DUP_ATTR("name", "event declaration");
3049 _SET(set
, _EVENT_NAME_SET
);
3050 } else if (!strcmp(left
, "id")) {
3053 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3054 _PERROR_DUP_ATTR("id", "event declaration");
3059 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3061 /* Only read "id" if get_unary_unsigned() succeeded. */
3062 if (ret
|| (!ret
&& id
< 0)) {
3063 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3068 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3071 "cannot set event declaration's ID");
3075 _SET(set
, _EVENT_ID_SET
);
3076 } else if (!strcmp(left
, "stream_id")) {
3077 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3078 _PERROR_DUP_ATTR("stream_id",
3079 "event declaration");
3084 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3085 (uint64_t *) stream_id
);
3087 * Only read "stream_id" if get_unary_unsigned()
3090 if (ret
|| (!ret
&& *stream_id
< 0)) {
3091 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3096 _SET(set
, _EVENT_STREAM_ID_SET
);
3097 } else if (!strcmp(left
, "context")) {
3098 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3099 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3104 ret
= visit_type_specifier_list(ctx
,
3105 _BT_LIST_FIRST_ENTRY(
3106 &node
->u
.ctf_expression
.right
,
3107 struct ctf_node
, siblings
),
3110 _PERROR("%s", "cannot create event context declaration");
3115 ret
= bt_ctf_event_class_set_context_type(
3119 _PERROR("%s", "cannot set event's context declaration");
3123 _SET(set
, _EVENT_CONTEXT_SET
);
3124 } else if (!strcmp(left
, "fields")) {
3125 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3126 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3131 ret
= visit_type_specifier_list(ctx
,
3132 _BT_LIST_FIRST_ENTRY(
3133 &node
->u
.ctf_expression
.right
,
3134 struct ctf_node
, siblings
),
3137 _PERROR("%s", "cannot create event payload field type");
3142 ret
= bt_ctf_event_class_set_payload_type(
3146 _PERROR("%s", "cannot set event's payload field type");
3150 _SET(set
, _EVENT_FIELDS_SET
);
3151 } else if (!strcmp(left
, "loglevel")) {
3152 uint64_t loglevel_value
;
3153 const char *loglevel_str
;
3154 struct bt_value
*value_obj
, *str_obj
;
3156 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3157 _PERROR_DUP_ATTR("loglevel",
3158 "event declaration");
3163 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3166 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3170 value_obj
= bt_value_integer_create_init(loglevel_value
);
3172 _PERROR("%s", "cannot allocate memory for loglevel value object");
3176 if (bt_ctf_event_class_set_attribute(event_class
,
3177 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3178 _PERROR("%s", "cannot set loglevel value");
3183 loglevel_str
= print_loglevel(loglevel_value
);
3185 str_obj
= bt_value_string_create_init(loglevel_str
);
3186 if (bt_ctf_event_class_set_attribute(event_class
,
3187 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3188 _PERROR("%s", "cannot set loglevel string");
3196 _SET(set
, _EVENT_LOGLEVEL_SET
);
3197 } else if (!strcmp(left
, "model.emf.uri")) {
3200 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3201 _PERROR_DUP_ATTR("model.emf.uri",
3202 "event declaration");
3207 right
= concatenate_unary_strings(
3208 &node
->u
.ctf_expression
.right
);
3210 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3215 // TODO: FIXME: set model EMF URI here
3218 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3220 _PWARNING("unknown attribute \"%s\" in event declaration",
3246 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3250 struct ctf_node
*iter
;
3251 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3253 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3254 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3258 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3263 if (!strcmp(left
, "name")) {
3264 name
= concatenate_unary_strings(
3265 &iter
->u
.ctf_expression
.right
);
3267 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3289 int reset_event_decl_types(struct ctx
*ctx
,
3290 struct bt_ctf_event_class
*event_class
)
3295 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3297 _PERROR("%s", "cannot set initial NULL event context");
3301 /* Event payload. */
3302 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3304 _PERROR("%s", "cannot set initial NULL event payload");
3312 int reset_stream_decl_types(struct ctx
*ctx
,
3313 struct bt_ctf_stream_class
*stream_class
)
3317 /* Packet context. */
3318 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3320 _PERROR("%s", "cannot set initial empty packet context");
3325 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3327 _PERROR("%s", "cannot set initial empty event header");
3331 /* Event context. */
3332 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3334 _PERROR("%s", "cannot set initial empty stream event context");
3342 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3345 struct bt_ctf_stream_class
*stream_class
;
3347 stream_class
= bt_ctf_stream_class_create(NULL
);
3348 if (!stream_class
) {
3349 _PERROR("%s", "cannot create stream class");
3354 * Set packet context, event header, and event context to NULL to
3355 * override the default ones.
3357 ret
= reset_stream_decl_types(ctx
, stream_class
);
3362 return stream_class
;
3365 BT_PUT(stream_class
);
3371 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3376 struct ctf_node
*iter
;
3377 int64_t stream_id
= -1;
3378 char *event_name
= NULL
;
3379 struct bt_ctf_event_class
*event_class
= NULL
;
3380 struct bt_ctf_event_class
*eevent_class
;
3381 struct bt_ctf_stream_class
*stream_class
= NULL
;
3382 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3383 bool pop_scope
= false;
3385 if (node
->visited
) {
3389 node
->visited
= TRUE
;
3390 event_name
= get_event_decl_name(ctx
, node
);
3393 "missing \"name\" attribute in event declaration");
3398 event_class
= bt_ctf_event_class_create(event_name
);
3401 * Unset context and fields to override the default ones.
3403 ret
= reset_event_decl_types(ctx
, event_class
);
3408 ret
= ctx_push_scope(ctx
);
3410 _PERROR("%s", "cannot push scope");
3416 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3417 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3424 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3426 struct bt_ctf_stream_class
*new_stream_class
;
3427 size_t stream_class_count
=
3428 g_hash_table_size(ctx
->stream_classes
) +
3429 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3432 * Allow missing stream_id if there is only a single
3435 switch (stream_class_count
) {
3437 /* Create implicit stream class if there's none */
3438 new_stream_class
= create_reset_stream_class(ctx
);
3439 if (!new_stream_class
) {
3444 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3446 _PERROR("%s", "cannot set stream class's ID");
3447 BT_PUT(new_stream_class
);
3453 /* Move reference to visitor's context */
3454 g_hash_table_insert(ctx
->stream_classes
,
3455 (gpointer
) stream_id
, new_stream_class
);
3456 new_stream_class
= NULL
;
3459 /* Single stream class: get its ID */
3460 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3461 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3462 stream_id
= (int64_t) keys
->data
;
3465 assert(bt_ctf_trace_get_stream_class_count(
3468 bt_ctf_trace_get_stream_class_by_index(
3470 assert(stream_class
);
3471 stream_id
= bt_ctf_stream_class_get_id(
3473 BT_PUT(stream_class
);
3477 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3483 assert(stream_id
>= 0);
3485 /* We have the stream ID now; get the stream class if found */
3486 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3487 (gpointer
) stream_id
);
3488 bt_get(stream_class
);
3489 if (!stream_class
) {
3490 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3492 if (!stream_class
) {
3493 _PERROR("cannot find stream class with ID %" PRId64
,
3500 assert(stream_class
);
3502 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3503 /* Allow only one event without ID per stream */
3504 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3507 "missing \"id\" field in event declaration");
3513 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3515 _PERROR("%s", "cannot set event's ID");
3520 event_id
= bt_ctf_event_class_get_id(event_class
);
3522 _PERROR("%s", "cannot get event's ID");
3527 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3530 BT_PUT(eevent_class
);
3531 _PERROR("duplicate event with ID %" PRId64
" in same stream", event_id
);
3536 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3537 BT_PUT(event_class
);
3539 _PERROR("%s", "cannot add event class to stream class");
3546 bt_put(event_class
);
3554 bt_put(stream_class
);
3559 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3560 struct bt_ctf_stream_class
*stream_class
, int *set
)
3564 _BT_CTF_FIELD_TYPE_INIT(decl
);
3566 switch (node
->type
) {
3568 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3569 &node
->u
._typedef
.type_declarators
);
3572 "cannot add typedef in \"stream\" declaration");
3576 case NODE_TYPEALIAS
:
3577 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3578 node
->u
.typealias
.alias
);
3580 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3584 case NODE_CTF_EXPRESSION
:
3586 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3592 if (!strcmp(left
, "id")) {
3596 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3597 _PERROR_DUP_ATTR("id", "stream declaration");
3602 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3604 /* Only read "id" if get_unary_unsigned() succeeded. */
3605 if (ret
|| (!ret
&& id
< 0)) {
3606 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3611 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3614 _PERROR("duplicate stream with ID %" PRId64
,
3620 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3623 "cannot set stream class's ID");
3627 _SET(set
, _STREAM_ID_SET
);
3628 } else if (!strcmp(left
, "event.header")) {
3629 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3630 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3635 ret
= visit_type_specifier_list(ctx
,
3636 _BT_LIST_FIRST_ENTRY(
3637 &node
->u
.ctf_expression
.right
,
3638 struct ctf_node
, siblings
),
3641 _PERROR("%s", "cannot create event header field type");
3647 ret
= bt_ctf_stream_class_set_event_header_type(
3648 stream_class
, decl
);
3651 _PERROR("%s", "cannot set stream's event header field type");
3655 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3656 } else if (!strcmp(left
, "event.context")) {
3657 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3658 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3663 ret
= visit_type_specifier_list(ctx
,
3664 _BT_LIST_FIRST_ENTRY(
3665 &node
->u
.ctf_expression
.right
,
3666 struct ctf_node
, siblings
),
3669 _PERROR("%s", "cannot create stream event context field type");
3675 ret
= bt_ctf_stream_class_set_event_context_type(
3676 stream_class
, decl
);
3679 _PERROR("%s", "cannot set stream's event context field type");
3683 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3684 } else if (!strcmp(left
, "packet.context")) {
3685 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3686 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3691 ret
= visit_type_specifier_list(ctx
,
3692 _BT_LIST_FIRST_ENTRY(
3693 &node
->u
.ctf_expression
.right
,
3694 struct ctf_node
, siblings
),
3697 _PERROR("%s", "cannot create packet context field type");
3703 ret
= bt_ctf_stream_class_set_packet_context_type(
3704 stream_class
, decl
);
3707 _PERROR("%s", "cannot set stream's packet context field type");
3711 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3713 _PWARNING("unknown attribute \"%s\" in stream declaration",
3737 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3742 struct ctf_node
*iter
;
3743 struct bt_ctf_stream_class
*stream_class
= NULL
;
3744 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3745 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3747 if (node
->visited
) {
3751 node
->visited
= TRUE
;
3752 stream_class
= create_reset_stream_class(ctx
);
3753 if (!stream_class
) {
3758 ret
= ctx_push_scope(ctx
);
3760 _PERROR("%s", "cannot push scope");
3764 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3765 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3774 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3775 /* Check that packet header has stream_id field */
3776 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3777 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3779 packet_header_decl
=
3780 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3781 if (!packet_header_decl
) {
3783 "cannot get trace packet header field type");
3788 bt_ctf_field_type_structure_get_field_type_by_name(
3789 packet_header_decl
, "stream_id");
3790 BT_PUT(packet_header_decl
);
3791 if (!stream_id_decl
) {
3792 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3796 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3797 BT_PUT(stream_id_decl
);
3798 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3802 BT_PUT(stream_id_decl
);
3804 /* Allow only _one_ ID-less stream */
3805 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3807 "missing \"id\" field in stream declaration");
3812 /* Automatic ID: 0 */
3813 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3816 id
= bt_ctf_stream_class_get_id(stream_class
);
3818 _PERROR("wrong stream ID: %" PRId64
, id
);
3824 * Make sure that this stream class's ID is currently unique in
3827 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
)) {
3828 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3833 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3835 if (existing_stream_class
) {
3836 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3841 /* Move reference to visitor's context */
3842 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3844 stream_class
= NULL
;
3848 bt_put(stream_class
);
3851 bt_put(existing_stream_class
);
3856 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3860 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3862 switch (node
->type
) {
3864 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3865 &node
->u
._typedef
.type_declarators
);
3868 "cannot add typedef in \"trace\" declaration");
3872 case NODE_TYPEALIAS
:
3873 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3874 node
->u
.typealias
.alias
);
3877 "cannot add typealias in \"trace\" declaration");
3881 case NODE_CTF_EXPRESSION
:
3883 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3889 if (!strcmp(left
, "major")) {
3890 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3891 _PERROR_DUP_ATTR("major", "trace declaration");
3896 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3899 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3904 _SET(set
, _TRACE_MAJOR_SET
);
3905 } else if (!strcmp(left
, "minor")) {
3906 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3907 _PERROR_DUP_ATTR("minor", "trace declaration");
3912 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3915 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3920 _SET(set
, _TRACE_MINOR_SET
);
3921 } else if (!strcmp(left
, "uuid")) {
3922 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3923 _PERROR_DUP_ATTR("uuid", "trace declaration");
3928 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3932 "invalid trace declaration's UUID");
3936 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
3939 "cannot set trace's UUID");
3943 _SET(set
, _TRACE_UUID_SET
);
3944 } else if (!strcmp(left
, "byte_order")) {
3945 /* Native byte order is already known at this stage */
3946 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3947 _PERROR_DUP_ATTR("byte_order",
3948 "trace declaration");
3953 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3954 } else if (!strcmp(left
, "packet.header")) {
3955 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3956 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3961 ret
= visit_type_specifier_list(ctx
,
3962 _BT_LIST_FIRST_ENTRY(
3963 &node
->u
.ctf_expression
.right
,
3964 struct ctf_node
, siblings
),
3965 &packet_header_decl
);
3967 _PERROR("%s", "cannot create packet header field type");
3971 assert(packet_header_decl
);
3972 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3973 packet_header_decl
);
3974 BT_PUT(packet_header_decl
);
3976 _PERROR("%s", "cannot set trace's packet header field type");
3980 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3982 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3990 _PERROR("%s", "unknown expression in trace declaration");
3999 BT_PUT(packet_header_decl
);
4005 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
4009 struct ctf_node
*iter
;
4010 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
4012 if (node
->visited
) {
4016 node
->visited
= TRUE
;
4018 if (ctx
->is_trace_visited
) {
4019 _PERROR("%s", "duplicate \"trace\" block");
4024 ret
= ctx_push_scope(ctx
);
4026 _PERROR("%s", "cannot push scope");
4030 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4031 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4040 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4042 "missing \"major\" attribute in trace declaration");
4047 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4049 "missing \"minor\" attribute in trace declaration");
4054 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4055 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4060 ctx
->is_trace_visited
= TRUE
;
4070 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4074 struct ctf_node
*entry_node
;
4075 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4077 if (node
->visited
) {
4081 node
->visited
= TRUE
;
4083 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4084 struct bt_list_head
*right_head
=
4085 &entry_node
->u
.ctf_expression
.right
;
4087 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4088 _PERROR("%s", "wrong expression in environment entry");
4093 left
= concatenate_unary_strings(
4094 &entry_node
->u
.ctf_expression
.left
);
4096 _PERROR("%s", "cannot get environment entry name");
4101 if (is_unary_string(right_head
)) {
4102 char *right
= concatenate_unary_strings(right_head
);
4105 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4111 if (strcmp(left
, "tracer_name") == 0) {
4112 if (strncmp(right
, "lttng", 5) == 0) {
4117 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4118 ret
= bt_ctf_trace_set_environment_field_string(
4119 ctx
->trace
, left
, right
);
4123 _PERROR("environment: cannot add entry \"%s\" to trace",
4127 } else if (is_unary_unsigned(right_head
) ||
4128 is_unary_signed(right_head
)) {
4131 if (is_unary_unsigned(right_head
)) {
4132 ret
= get_unary_unsigned(right_head
,
4135 ret
= get_unary_signed(right_head
, &v
);
4138 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4144 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4145 ret
= bt_ctf_trace_set_environment_field_integer(
4146 ctx
->trace
, left
, v
);
4148 _PERROR("environment: cannot add entry \"%s\" to trace",
4153 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4171 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4176 struct ctf_node
*node
;
4177 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4179 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4180 if (node
->type
== NODE_CTF_EXPRESSION
) {
4181 struct ctf_node
*right_node
;
4183 left
= concatenate_unary_strings(
4184 &node
->u
.ctf_expression
.left
);
4190 if (!strcmp(left
, "byte_order")) {
4191 enum bt_ctf_byte_order bo
;
4193 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4194 _PERROR_DUP_ATTR("byte_order",
4195 "trace declaration");
4200 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4201 right_node
= _BT_LIST_FIRST_ENTRY(
4202 &node
->u
.ctf_expression
.right
,
4203 struct ctf_node
, siblings
);
4204 bo
= byte_order_from_unary_expr(ctx
->efd
,
4206 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4207 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4210 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4211 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4217 ret
= bt_ctf_trace_set_native_byte_order(
4220 _PERROR("cannot set trace's byte order (%d)",
4231 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4232 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4246 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4247 struct bt_ctf_clock_class
*clock
, int *set
)
4252 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4257 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4263 if (!strcmp(left
, "name")) {
4266 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4267 _PERROR_DUP_ATTR("name", "clock class declaration");
4272 right
= concatenate_unary_strings(
4273 &entry_node
->u
.ctf_expression
.right
);
4275 _PERROR("%s", "unexpected unary expression for clock class declaration's \"name\" attribute");
4280 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4282 _PERROR("%s", "cannot set clock class's name");
4288 _SET(set
, _CLOCK_NAME_SET
);
4289 } else if (!strcmp(left
, "uuid")) {
4290 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4292 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4293 _PERROR_DUP_ATTR("uuid", "clock class declaration");
4298 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4300 _PERROR("%s", "invalid clock class UUID");
4304 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4306 _PERROR("%s", "cannot set clock class's UUID");
4310 _SET(set
, _CLOCK_UUID_SET
);
4311 } else if (!strcmp(left
, "description")) {
4314 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4315 _PERROR_DUP_ATTR("description", "clock class declaration");
4320 right
= concatenate_unary_strings(
4321 &entry_node
->u
.ctf_expression
.right
);
4323 _PERROR("%s", "unexpected unary expression for clock class's \"description\" attribute");
4328 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4330 _PERROR("%s", "cannot set clock class's description");
4336 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4337 } else if (!strcmp(left
, "freq")) {
4340 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4341 _PERROR_DUP_ATTR("freq", "clock class declaration");
4346 ret
= get_unary_unsigned(
4347 &entry_node
->u
.ctf_expression
.right
, &freq
);
4349 _PERROR("%s", "unexpected unary expression for clock class declaration's \"freq\" attribute");
4354 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4356 _PERROR("%s", "cannot set clock class's frequency");
4360 _SET(set
, _CLOCK_FREQ_SET
);
4361 } else if (!strcmp(left
, "precision")) {
4364 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4365 _PERROR_DUP_ATTR("precision", "clock class declaration");
4370 ret
= get_unary_unsigned(
4371 &entry_node
->u
.ctf_expression
.right
, &precision
);
4373 _PERROR("%s", "unexpected unary expression for clock class declaration's \"precision\" attribute");
4378 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4380 _PERROR("%s", "cannot set clock class's precision");
4384 _SET(set
, _CLOCK_PRECISION_SET
);
4385 } else if (!strcmp(left
, "offset_s")) {
4388 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4389 _PERROR_DUP_ATTR("offset_s", "clock class declaration");
4394 ret
= get_unary_unsigned(
4395 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4397 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset_s\" attribute");
4402 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4404 _PERROR("%s", "cannot set clock class's offset in seconds");
4408 _SET(set
, _CLOCK_OFFSET_S_SET
);
4409 } else if (!strcmp(left
, "offset")) {
4412 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4413 _PERROR_DUP_ATTR("offset", "clock class declaration");
4418 ret
= get_unary_unsigned(
4419 &entry_node
->u
.ctf_expression
.right
, &offset
);
4421 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset\" attribute");
4426 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4428 _PERROR("%s", "cannot set clock class's offset in cycles");
4432 _SET(set
, _CLOCK_OFFSET_SET
);
4433 } else if (!strcmp(left
, "absolute")) {
4434 struct ctf_node
*right
;
4436 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4437 _PERROR_DUP_ATTR("absolute", "clock class declaration");
4442 right
= _BT_LIST_FIRST_ENTRY(
4443 &entry_node
->u
.ctf_expression
.right
,
4444 struct ctf_node
, siblings
);
4445 ret
= get_boolean(ctx
->efd
, right
);
4447 _PERROR("%s", "unexpected unary expression for clock class declaration's \"absolute\" attribute");
4452 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4454 _PERROR("%s", "cannot set clock class's absolute option");
4458 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4460 _PWARNING("unknown attribute \"%s\" in clock class declaration",
4476 uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4481 if (frequency
== 1000000000ULL) {
4484 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4491 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4495 int64_t offset_cycles
;
4497 freq
= bt_ctf_clock_class_get_frequency(clock
);
4498 if (freq
== -1ULL) {
4499 _PERROR("%s", "cannot get clock class frequency");
4504 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4506 _PERROR("%s", "cannot get offset in cycles");
4511 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4512 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4519 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4523 struct bt_ctf_clock_class
*clock
;
4524 struct ctf_node
*entry_node
;
4525 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4526 const char *clock_class_name
;
4528 if (clock_node
->visited
) {
4532 clock_node
->visited
= TRUE
;
4533 clock
= bt_ctf_clock_class_create(NULL
);
4535 _PERROR("%s", "cannot create clock");
4540 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4541 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4547 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4549 "missing \"name\" attribute in clock class declaration");
4554 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4555 assert(clock_class_name
);
4556 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4558 * Old versions of LTTng forgot to set its clock class
4559 * as absolute, even if it is. This is important because
4560 * it's a condition to be able to sort notifications
4561 * from different sources.
4563 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4565 _PERROR("%s", "cannot set clock class's absolute option");
4570 ret
= apply_clock_class_offset(ctx
, clock
);
4572 _PERROR("%s", "cannot apply clock class offset ");
4576 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4578 _PERROR("%s", "cannot add clock class to trace");
4589 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4593 if (root_decl_node
->visited
) {
4597 root_decl_node
->visited
= TRUE
;
4599 switch (root_decl_node
->type
) {
4601 ret
= visit_typedef(ctx
,
4602 root_decl_node
->u
._typedef
.type_specifier_list
,
4603 &root_decl_node
->u
._typedef
.type_declarators
);
4605 _PERROR("%s", "cannot add typedef in root scope");
4609 case NODE_TYPEALIAS
:
4610 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4611 root_decl_node
->u
.typealias
.alias
);
4613 _PERROR("%s", "cannot add typealias in root scope");
4617 case NODE_TYPE_SPECIFIER_LIST
:
4619 _BT_CTF_FIELD_TYPE_INIT(decl
);
4622 * Just add the type specifier to the root
4623 * declaration scope. Put local reference.
4625 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4644 int set_trace_name(struct ctx
*ctx
)
4648 struct bt_value
*value
= NULL
;
4650 assert(bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0);
4651 name
= g_string_new(NULL
);
4658 * Check if we have a trace environment string value named `hostname`.
4659 * If so, use it as the trace name's prefix.
4661 value
= bt_ctf_trace_get_environment_field_value_by_name(ctx
->trace
,
4663 if (bt_value_is_string(value
)) {
4664 const char *hostname
;
4666 ret
= bt_value_string_get(value
, &hostname
);
4668 g_string_append(name
, hostname
);
4670 if (ctx
->trace_name_suffix
) {
4671 g_string_append_c(name
, G_DIR_SEPARATOR
);
4675 if (ctx
->trace_name_suffix
) {
4676 g_string_append(name
, ctx
->trace_name_suffix
);
4679 ret
= bt_ctf_trace_set_name(ctx
->trace
, name
->str
);
4693 g_string_free(name
, TRUE
);
4700 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4703 GHashTableIter iter
;
4704 gpointer key
, stream_class
;
4706 if (g_hash_table_size(ctx
->stream_classes
) > 0 &&
4707 bt_ctf_trace_get_stream_class_count(ctx
->trace
) == 0) {
4709 * We're about to add the first stream class to the
4710 * trace. This will freeze the trace, and after this
4711 * we cannot set the name anymore. At this point,
4712 * set the trace name.
4714 ret
= set_trace_name(ctx
);
4720 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4722 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4723 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4726 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4727 _PERROR("cannot add stream class %" PRId64
" to trace",
4733 g_hash_table_remove_all(ctx
->stream_classes
);
4740 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(FILE *efd
,
4741 uint64_t clock_class_offset_ns
, const char *name
)
4744 struct ctx
*ctx
= NULL
;
4745 struct bt_ctf_trace
*trace
;
4747 trace
= bt_ctf_trace_create();
4749 _FPERROR(efd
, "%s", "cannot create trace");
4753 /* Set packet header to NULL to override the default one */
4754 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
4757 "cannot set initial, empty packet header structure");
4761 /* Create visitor's context */
4762 ctx
= ctx_create(trace
, efd
, clock_class_offset_ns
, name
);
4764 _FPERROR(efd
, "%s", "cannot create visitor context");
4777 return (void *) ctx
;
4781 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4783 ctx_destroy((void *) visitor
);
4787 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
4788 struct ctf_visitor_generate_ir
*visitor
)
4790 struct ctx
*ctx
= (void *) visitor
;
4794 return bt_get(ctx
->trace
);
4798 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4799 struct ctf_node
*node
)
4802 struct ctx
*ctx
= (void *) visitor
;
4804 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4806 switch (node
->type
) {
4809 struct ctf_node
*iter
;
4810 int got_trace_decl
= FALSE
;
4811 int found_callsite
= FALSE
;
4814 * The first thing we need is the native byte order of
4815 * the trace block, because early type aliases can have
4816 * a `byte_order` attribute set to `native`. If we don't
4817 * have the native byte order yet, and we don't have any
4818 * trace block yet, then fail with EINCOMPLETE.
4820 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4821 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4822 if (got_trace_decl
) {
4823 _PERROR("%s", "duplicate trace declaration");
4828 ret
= set_trace_byte_order(ctx
, iter
);
4830 _PERROR("cannot set trace's native byte order (%d)",
4835 got_trace_decl
= TRUE
;
4838 if (!got_trace_decl
) {
4844 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
4845 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
4846 assert(ctx
->current_scope
&&
4847 ctx
->current_scope
->parent_scope
== NULL
);
4850 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4851 ret
= visit_env(ctx
, iter
);
4853 _PERROR("error while visiting environment block (%d)",
4859 assert(ctx
->current_scope
&&
4860 ctx
->current_scope
->parent_scope
== NULL
);
4863 * Visit clock blocks.
4865 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4866 ret
= visit_clock_decl(ctx
, iter
);
4868 _PERROR("error while visiting clock class declaration (%d)",
4874 assert(ctx
->current_scope
&&
4875 ctx
->current_scope
->parent_scope
== NULL
);
4878 * Visit root declarations next, as they can be used by any
4881 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4883 ret
= visit_root_decl(ctx
, iter
);
4885 _PERROR("error while visiting root declaration (%d)",
4891 assert(ctx
->current_scope
&&
4892 ctx
->current_scope
->parent_scope
== NULL
);
4894 /* Callsite blocks are not supported */
4895 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4896 found_callsite
= TRUE
;
4900 assert(ctx
->current_scope
&&
4901 ctx
->current_scope
->parent_scope
== NULL
);
4903 if (found_callsite
) {
4904 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4908 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4909 ret
= visit_trace_decl(ctx
, iter
);
4911 _PERROR("%s", "error while visiting trace declaration");
4916 assert(ctx
->current_scope
&&
4917 ctx
->current_scope
->parent_scope
== NULL
);
4920 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4921 ret
= visit_stream_decl(ctx
, iter
);
4923 _PERROR("%s", "error while visiting stream declaration");
4928 assert(ctx
->current_scope
&&
4929 ctx
->current_scope
->parent_scope
== NULL
);
4932 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4933 ret
= visit_event_decl(ctx
, iter
);
4935 _PERROR("%s", "error while visiting event declaration");
4940 assert(ctx
->current_scope
&&
4941 ctx
->current_scope
->parent_scope
== NULL
);
4945 _PERROR("cannot decode node type: %d", (int) node
->type
);
4950 /* Move decoded stream classes to trace, if any */
4951 ret
= move_ctx_stream_classes_to_trace(ctx
);
4953 _PERROR("%s", "cannot move stream classes to trace");