2 * ctf-visitor-generate-ir.c
4 * Common Trace Format metadata visitor (generates CTF IR objects).
6 * Based on older ctf-visitor-generate-io-struct.c.
8 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Copyright 2015-2016 - Philippe Proulx <philippe.proulx@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40 #include <babeltrace/compat/uuid-internal.h>
41 #include <babeltrace/endian-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/ctf-ir/trace.h>
44 #include <babeltrace/ctf-ir/stream-class.h>
45 #include <babeltrace/ctf-ir/event.h>
46 #include <babeltrace/ctf-ir/event-class.h>
47 #include <babeltrace/ctf-ir/field-types.h>
48 #include <babeltrace/ctf-ir/field-types-internal.h>
49 #include <babeltrace/ctf-ir/clock-class.h>
55 /* Bit value (left shift) */
56 #define _BV(_val) (1 << (_val))
58 /* Bit is set in a set of bits */
59 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
61 /* Set bit in a set of bits */
62 #define _SET(_set, _mask) (*(_set) |= (_mask))
64 /* Bits for verifying existing attributes in various declarations */
66 _CLOCK_NAME_SET
= _BV(0),
67 _CLOCK_UUID_SET
= _BV(1),
68 _CLOCK_FREQ_SET
= _BV(2),
69 _CLOCK_PRECISION_SET
= _BV(3),
70 _CLOCK_OFFSET_S_SET
= _BV(4),
71 _CLOCK_OFFSET_SET
= _BV(5),
72 _CLOCK_ABSOLUTE_SET
= _BV(6),
73 _CLOCK_DESCRIPTION_SET
= _BV(7),
77 _INTEGER_ALIGN_SET
= _BV(0),
78 _INTEGER_SIZE_SET
= _BV(1),
79 _INTEGER_BASE_SET
= _BV(2),
80 _INTEGER_ENCODING_SET
= _BV(3),
81 _INTEGER_BYTE_ORDER_SET
= _BV(4),
82 _INTEGER_SIGNED_SET
= _BV(5),
83 _INTEGER_MAP_SET
= _BV(6),
87 _FLOAT_ALIGN_SET
= _BV(0),
88 _FLOAT_MANT_DIG_SET
= _BV(1),
89 _FLOAT_EXP_DIG_SET
= _BV(2),
90 _FLOAT_BYTE_ORDER_SET
= _BV(3),
94 _STRING_ENCODING_SET
= _BV(0),
98 _TRACE_MINOR_SET
= _BV(0),
99 _TRACE_MAJOR_SET
= _BV(1),
100 _TRACE_BYTE_ORDER_SET
= _BV(2),
101 _TRACE_UUID_SET
= _BV(3),
102 _TRACE_PACKET_HEADER_SET
= _BV(4),
106 _STREAM_ID_SET
= _BV(0),
107 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
108 _STREAM_EVENT_HEADER_SET
= _BV(2),
109 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
113 _EVENT_NAME_SET
= _BV(0),
114 _EVENT_ID_SET
= _BV(1),
115 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
116 _EVENT_STREAM_ID_SET
= _BV(3),
117 _EVENT_LOGLEVEL_SET
= _BV(4),
118 _EVENT_CONTEXT_SET
= _BV(5),
119 _EVENT_FIELDS_SET
= _BV(6),
127 LOGLEVEL_WARNING
= 4,
130 LOGLEVEL_DEBUG_SYSTEM
= 7,
131 LOGLEVEL_DEBUG_PROGRAM
= 8,
132 LOGLEVEL_DEBUG_PROCESS
= 9,
133 LOGLEVEL_DEBUG_MODULE
= 10,
134 LOGLEVEL_DEBUG_UNIT
= 11,
135 LOGLEVEL_DEBUG_FUNCTION
= 12,
136 LOGLEVEL_DEBUG_LINE
= 13,
141 /* Prefixes of type aliases */
142 #define _PREFIX_ALIAS 'a'
143 #define _PREFIX_ENUM 'e'
144 #define _PREFIX_STRUCT 's'
145 #define _PREFIX_VARIANT 'v'
147 /* First entry in a BT list */
148 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
149 bt_list_entry((_ptr)->next, _type, _member)
151 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
153 /* Error printing wrappers */
154 #define _PERROR(_fmt, ...) \
156 if (!ctx->efd) break; \
157 fprintf(ctx->efd, "[error] %s: " _fmt "\n", \
158 __func__, __VA_ARGS__); \
161 #define _PWARNING(_fmt, ...) \
163 if (!ctx->efd) break; \
164 fprintf(ctx->efd, "[warning] %s: " _fmt "\n", \
165 __func__, __VA_ARGS__); \
168 #define _FPERROR(_stream, _fmt, ...) \
170 if (!_stream) break; \
171 fprintf(_stream, "[error] %s: " _fmt "\n", \
172 __func__, __VA_ARGS__); \
175 #define _FPWARNING(_stream, _fmt, ...) \
177 if (!_stream) break; \
178 fprintf(_stream, "[warning] %s: " _fmt "\n", \
179 __func__, __VA_ARGS__); \
182 #define _PERROR_DUP_ATTR(_attr, _entity) \
184 if (!ctx->efd) break; \
186 "[error] %s: duplicate attribute \"" \
187 _attr "\" in " _entity "\n", __func__); \
191 * Declaration scope of a visitor context. This represents a TSDL
192 * lexical scope, so that aliases and named structures, variants,
193 * and enumerations may be registered and looked up hierarchically.
195 struct ctx_decl_scope
{
197 * Alias name to field type.
199 * GQuark -> struct bt_ctf_field_type *
201 GHashTable
*decl_map
;
203 /* Parent scope; NULL if this is the root declaration scope */
204 struct ctx_decl_scope
*parent_scope
;
208 * Visitor context (private).
211 /* Trace being filled (owned by this) */
212 struct bt_ctf_trace
*trace
;
214 /* Error stream to use during visit */
217 /* Current declaration scope (top of the stack) */
218 struct ctx_decl_scope
*current_scope
;
220 /* 1 if trace declaration is visited */
221 int is_trace_visited
;
223 /* 1 if this is an LTTng trace */
226 /* Offset (ns) to apply to clock classes on creation */
227 uint64_t clock_class_offset_ns
;
229 /* Trace attributes */
230 enum bt_ctf_byte_order trace_bo
;
231 uint64_t trace_major
;
232 uint64_t trace_minor
;
233 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
236 * Stream IDs to stream classes.
238 * int64_t -> struct bt_ctf_stream_class *
240 GHashTable
*stream_classes
;
246 struct ctf_visitor_generate_ir
{ };
249 const char *loglevel_str
[] = {
250 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
251 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
252 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
253 [ LOGLEVEL_ERR
] = "TRACE_ERR",
254 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
255 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
256 [ LOGLEVEL_INFO
] = "TRACE_INFO",
257 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
258 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
259 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
260 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
261 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
262 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
263 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
264 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
268 const char *print_loglevel(int64_t value
)
273 if (value
>= _NR_LOGLEVELS
) {
274 return "<<UNKNOWN>>";
276 return loglevel_str
[value
];
280 * Creates a new declaration scope.
282 * @param par_scope Parent scope (NULL if creating a root scope)
283 * @returns New declaration scope, or NULL on error
286 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
288 struct ctx_decl_scope
*scope
;
290 scope
= g_new(struct ctx_decl_scope
, 1);
295 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
296 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
297 scope
->parent_scope
= par_scope
;
304 * Destroys a declaration scope.
306 * This function does not destroy the parent scope.
308 * @param scope Scope to destroy
311 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
317 g_hash_table_destroy(scope
->decl_map
);
325 * Returns the GQuark of a prefixed alias.
327 * @param prefix Prefix character
329 * @returns Associated GQuark, or 0 on error
332 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
338 /* Prefix character + original string + '\0' */
339 char *prname
= g_new(char, strlen(name
) + 2);
344 sprintf(prname
, "%c%s", prefix
, name
);
345 qname
= g_quark_from_string(prname
);
353 * Looks up a prefixed type alias within a declaration scope.
355 * @param scope Declaration scope
356 * @param prefix Prefix character
357 * @param name Alias name
358 * @param level Number of levels to dig (-1 means infinite)
359 * @returns Declaration, or NULL if not found
362 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
363 struct ctx_decl_scope
*scope
, char prefix
,
364 const char *name
, int levels
)
368 _BT_CTF_FIELD_TYPE_INIT(decl
);
369 struct ctx_decl_scope
*cur_scope
= scope
;
373 qname
= get_prefixed_named_quark(prefix
, name
);
382 while (cur_scope
&& cur_levels
< levels
) {
383 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
384 (gconstpointer
) (unsigned long) qname
);
386 /* Caller's reference */
391 cur_scope
= cur_scope
->parent_scope
;
402 * Looks up a type alias within a declaration scope.
404 * @param scope Declaration scope
405 * @param name Alias name
406 * @param level Number of levels to dig (-1 means infinite)
407 * @returns Declaration, or NULL if not found
410 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
411 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
413 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
418 * Looks up an enumeration within a declaration scope.
420 * @param scope Declaration scope
421 * @param name Enumeration name
422 * @param level Number of levels to dig (-1 means infinite)
423 * @returns Declaration, or NULL if not found
426 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
427 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
429 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
434 * Looks up a structure within a declaration scope.
436 * @param scope Declaration scope
437 * @param name Structure name
438 * @param level Number of levels to dig (-1 means infinite)
439 * @returns Declaration, or NULL if not found
442 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
443 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
445 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
450 * Looks up a variant within a declaration scope.
452 * @param scope Declaration scope
453 * @param name Variant name
454 * @param level Number of levels to dig (-1 means infinite)
455 * @returns Declaration, or NULL if not found
458 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
459 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
461 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
466 * Registers a prefixed type alias within a declaration scope.
468 * @param scope Declaration scope
469 * @param prefix Prefix character
470 * @param name Alias name (non-NULL)
471 * @param decl Declaration to register
472 * @returns 0 if registration went okay, negative value otherwise
475 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
476 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
480 _BT_CTF_FIELD_TYPE_INIT(edecl
);
485 qname
= get_prefixed_named_quark(prefix
, name
);
491 /* Make sure alias does not exist in local scope */
492 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
499 g_hash_table_insert(scope
->decl_map
,
500 (gpointer
) (unsigned long) qname
, decl
);
502 /* Hash table's reference */
512 * Registers a type alias within a declaration scope.
514 * @param scope Declaration scope
515 * @param name Alias name (non-NULL)
516 * @param decl Declaration to register
517 * @returns 0 if registration went okay, negative value otherwise
520 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
521 const char *name
, struct bt_ctf_field_type
*decl
)
523 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
528 * Registers an enumeration declaration within a declaration scope.
530 * @param scope Declaration scope
531 * @param name Enumeration name (non-NULL)
532 * @param decl Enumeration declaration to register
533 * @returns 0 if registration went okay, negative value otherwise
536 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
537 const char *name
, struct bt_ctf_field_type
*decl
)
539 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
544 * Registers a structure declaration within a declaration scope.
546 * @param scope Declaration scope
547 * @param name Structure name (non-NULL)
548 * @param decl Structure declaration to register
549 * @returns 0 if registration went okay, negative value otherwise
552 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
553 const char *name
, struct bt_ctf_field_type
*decl
)
555 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
560 * Registers a variant declaration within a declaration scope.
562 * @param scope Declaration scope
563 * @param name Variant name (non-NULL)
564 * @param decl Variant declaration to register
565 * @returns 0 if registration went okay, negative value otherwise
568 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
569 const char *name
, struct bt_ctf_field_type
*decl
)
571 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
576 * Creates a new visitor context.
578 * @param trace Associated trace
579 * @param efd Error stream
580 * @returns New visitor context, or NULL on error
583 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
,
584 uint64_t clock_class_offset_ns
)
586 struct ctx
*ctx
= NULL
;
587 struct ctx_decl_scope
*scope
= NULL
;
589 ctx
= g_new0(struct ctx
, 1);
594 /* Root declaration scope */
595 scope
= ctx_decl_scope_create(NULL
);
600 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
601 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
602 if (!ctx
->stream_classes
) {
608 ctx
->current_scope
= scope
;
609 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
610 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
615 ctx_decl_scope_destroy(scope
);
620 * Destroys a visitor context.
622 * @param ctx Visitor context to destroy
625 void ctx_destroy(struct ctx
*ctx
)
627 struct ctx_decl_scope
*scope
;
629 * Destroy all scopes, from current one to the root scope.
636 scope
= ctx
->current_scope
;
639 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
641 ctx_decl_scope_destroy(scope
);
642 scope
= parent_scope
;
646 g_hash_table_destroy(ctx
->stream_classes
);
654 * Pushes a new declaration scope on top of a visitor context's
655 * declaration scope stack.
657 * @param ctx Visitor context
658 * @returns 0 on success, or a negative value on error
661 int ctx_push_scope(struct ctx
*ctx
)
664 struct ctx_decl_scope
*new_scope
;
667 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
673 ctx
->current_scope
= new_scope
;
680 void ctx_pop_scope(struct ctx
*ctx
)
682 struct ctx_decl_scope
*parent_scope
= NULL
;
686 if (!ctx
->current_scope
) {
690 parent_scope
= ctx
->current_scope
->parent_scope
;
691 ctx_decl_scope_destroy(ctx
->current_scope
);
692 ctx
->current_scope
= parent_scope
;
699 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
700 struct bt_ctf_field_type
**decl
);
703 int is_unary_string(struct bt_list_head
*head
)
706 struct ctf_node
*node
;
708 bt_list_for_each_entry(node
, head
, siblings
) {
709 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
713 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
722 char *concatenate_unary_strings(struct bt_list_head
*head
)
726 struct ctf_node
*node
;
728 str
= g_string_new(NULL
);
730 bt_list_for_each_entry(node
, head
, siblings
) {
734 node
->type
!= NODE_UNARY_EXPRESSION
||
735 node
->u
.unary_expression
.type
!= UNARY_STRING
||
738 node
->u
.unary_expression
.link
!=
746 switch (node
->u
.unary_expression
.link
) {
748 g_string_append(str
, ".");
750 case UNARY_ARROWLINK
:
751 g_string_append(str
, "->");
753 case UNARY_DOTDOTDOT
:
754 g_string_append(str
, "...");
760 src_string
= node
->u
.unary_expression
.u
.string
;
761 g_string_append(str
, src_string
);
765 /* Destroys the container, returns the underlying string */
766 return g_string_free(str
, FALSE
);
769 /* This always returns NULL */
770 return g_string_free(str
, TRUE
);
774 const char *get_map_clock_name_value(struct bt_list_head
*head
)
777 struct ctf_node
*node
;
778 const char *name
= NULL
;
780 bt_list_for_each_entry(node
, head
, siblings
) {
782 int uexpr_type
= node
->u
.unary_expression
.type
;
783 int uexpr_link
= node
->u
.unary_expression
.link
;
784 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
785 uexpr_type
!= UNARY_STRING
||
786 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
791 /* Needs to be chained with . */
792 switch (node
->u
.unary_expression
.link
) {
795 case UNARY_ARROWLINK
:
796 case UNARY_DOTDOTDOT
:
802 src_string
= node
->u
.unary_expression
.u
.string
;
806 if (strcmp("clock", src_string
)) {
814 if (strcmp("value", src_string
)) {
819 /* Extra identifier, unknown */
833 int is_unary_unsigned(struct bt_list_head
*head
)
836 struct ctf_node
*node
;
838 bt_list_for_each_entry(node
, head
, siblings
) {
839 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
843 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
852 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
856 struct ctf_node
*node
;
858 bt_list_for_each_entry(node
, head
, siblings
) {
859 int uexpr_type
= node
->u
.unary_expression
.type
;
860 int uexpr_link
= node
->u
.unary_expression
.link
;
861 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
862 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
863 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
869 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
878 int is_unary_signed(struct bt_list_head
*head
)
881 struct ctf_node
*node
;
883 bt_list_for_each_entry(node
, head
, siblings
) {
884 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
888 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
897 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
901 struct ctf_node
*node
;
903 bt_list_for_each_entry(node
, head
, siblings
) {
904 int uexpr_type
= node
->u
.unary_expression
.type
;
905 int uexpr_link
= node
->u
.unary_expression
.link
;
906 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
907 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
908 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
909 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
910 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
916 switch (node
->u
.unary_expression
.type
) {
917 case UNARY_UNSIGNED_CONSTANT
:
919 node
->u
.unary_expression
.u
.unsigned_constant
;
921 case UNARY_SIGNED_CONSTANT
:
922 *value
= node
->u
.unary_expression
.u
.signed_constant
;
937 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
941 struct ctf_node
*node
;
943 bt_list_for_each_entry(node
, head
, siblings
) {
944 int uexpr_type
= node
->u
.unary_expression
.type
;
945 int uexpr_link
= node
->u
.unary_expression
.link
;
946 const char *src_string
;
948 if (node
->type
!= NODE_UNARY_EXPRESSION
||
949 uexpr_type
!= UNARY_STRING
||
950 uexpr_link
!= UNARY_LINK_UNKNOWN
||
956 src_string
= node
->u
.unary_expression
.u
.string
;
957 ret
= bt_uuid_parse(src_string
, uuid
);
968 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
972 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
973 _FPERROR(efd
, "%s", "expecting unary expression");
978 switch (unary_expr
->u
.unary_expression
.type
) {
979 case UNARY_UNSIGNED_CONSTANT
:
980 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
982 case UNARY_SIGNED_CONSTANT
:
983 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
987 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
989 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
991 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
994 _FPERROR(efd
, "unexpected string \"%s\"", str
);
1001 _FPERROR(efd
, "%s", "unexpected unary expression type");
1011 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
1012 struct ctf_node
*unary_expr
)
1015 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1017 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1019 "\"byte_order\" attribute: expecting string");
1023 str
= unary_expr
->u
.unary_expression
.u
.string
;
1025 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1026 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1027 } else if (!strcmp(str
, "le")) {
1028 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1029 } else if (!strcmp(str
, "native")) {
1030 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1032 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1042 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1043 struct ctf_node
*uexpr
)
1045 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
1047 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1048 bo
= bt_ctf_trace_get_native_byte_order(ctx
->trace
);
1055 int is_align_valid(uint64_t align
)
1057 return (align
!= 0) && !(align
& (align
- 1));
1061 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1066 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1071 switch (type_specifier
->u
.type_specifier
.type
) {
1073 g_string_append(str
, "void");
1076 g_string_append(str
, "char");
1078 case TYPESPEC_SHORT
:
1079 g_string_append(str
, "short");
1082 g_string_append(str
, "int");
1085 g_string_append(str
, "long");
1087 case TYPESPEC_FLOAT
:
1088 g_string_append(str
, "float");
1090 case TYPESPEC_DOUBLE
:
1091 g_string_append(str
, "double");
1093 case TYPESPEC_SIGNED
:
1094 g_string_append(str
, "signed");
1096 case TYPESPEC_UNSIGNED
:
1097 g_string_append(str
, "unsigned");
1100 g_string_append(str
, "bool");
1102 case TYPESPEC_COMPLEX
:
1103 g_string_append(str
, "_Complex");
1105 case TYPESPEC_IMAGINARY
:
1106 g_string_append(str
, "_Imaginary");
1108 case TYPESPEC_CONST
:
1109 g_string_append(str
, "const");
1111 case TYPESPEC_ID_TYPE
:
1112 if (type_specifier
->u
.type_specifier
.id_type
) {
1113 g_string_append(str
,
1114 type_specifier
->u
.type_specifier
.id_type
);
1117 case TYPESPEC_STRUCT
:
1119 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1121 if (!node
->u
._struct
.name
) {
1122 _PERROR("%s", "unexpected empty structure name");
1127 g_string_append(str
, "struct ");
1128 g_string_append(str
, node
->u
._struct
.name
);
1131 case TYPESPEC_VARIANT
:
1133 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1135 if (!node
->u
.variant
.name
) {
1136 _PERROR("%s", "unexpected empty variant name");
1141 g_string_append(str
, "variant ");
1142 g_string_append(str
, node
->u
.variant
.name
);
1147 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1149 if (!node
->u
._enum
.enum_id
) {
1150 _PERROR("%s", "unexpected empty enum name");
1155 g_string_append(str
, "enum ");
1156 g_string_append(str
, node
->u
._enum
.enum_id
);
1159 case TYPESPEC_FLOATING_POINT
:
1160 case TYPESPEC_INTEGER
:
1161 case TYPESPEC_STRING
:
1163 _PERROR("%s", "unknown specifier");
1173 int get_type_specifier_list_name(struct ctx
*ctx
,
1174 struct ctf_node
*type_specifier_list
, GString
*str
)
1177 struct ctf_node
*iter
;
1178 int alias_item_nr
= 0;
1179 struct bt_list_head
*head
=
1180 &type_specifier_list
->u
.type_specifier_list
.head
;
1182 bt_list_for_each_entry(iter
, head
, siblings
) {
1183 if (alias_item_nr
!= 0) {
1184 g_string_append(str
, " ");
1188 ret
= get_type_specifier_name(ctx
, iter
, str
);
1199 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1200 struct ctf_node
*type_specifier_list
,
1201 struct ctf_node
*node_type_declarator
)
1207 struct ctf_node
*iter
;
1208 struct bt_list_head
*pointers
=
1209 &node_type_declarator
->u
.type_declarator
.pointers
;
1211 str
= g_string_new("");
1212 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1214 g_string_free(str
, TRUE
);
1218 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1219 g_string_append(str
, " *");
1221 if (iter
->u
.pointer
.const_qualifier
) {
1222 g_string_append(str
, " const");
1226 str_c
= g_string_free(str
, FALSE
);
1227 qalias
= g_quark_from_string(str_c
);
1235 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1236 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1237 struct bt_ctf_field_type
**field_decl
,
1238 struct bt_ctf_field_type
*nested_decl
)
1241 * During this whole function, nested_decl is always OURS,
1242 * whereas field_decl is an output which we create, but
1243 * belongs to the caller (it is moved).
1249 /* Validate type declarator node */
1250 if (node_type_declarator
) {
1251 if (node_type_declarator
->u
.type_declarator
.type
==
1257 /* TODO: GCC bitfields not supported yet */
1258 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1260 _PERROR("%s", "GCC bitfields are not supported as of this version");
1266 /* Find the right nested declaration if not provided */
1268 struct bt_list_head
*pointers
=
1269 &node_type_declarator
->u
.type_declarator
.pointers
;
1271 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1273 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1276 * If we have a pointer declarator, it HAS to
1277 * be present in the typealiases (else fail).
1279 qalias
= create_typealias_identifier(ctx
,
1280 type_specifier_list
, node_type_declarator
);
1282 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1283 g_quark_to_string(qalias
), -1);
1285 _PERROR("cannot find typealias \"%s\"",
1286 g_quark_to_string(qalias
));
1291 /* Make a copy of it */
1292 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1293 BT_PUT(nested_decl
);
1294 if (!nested_decl_copy
) {
1295 _PERROR("%s", "cannot copy nested field type");
1300 BT_MOVE(nested_decl
, nested_decl_copy
);
1302 /* Force integer's base to 16 since it's a pointer */
1303 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1304 bt_ctf_field_type_integer_set_base(nested_decl
,
1305 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1308 ret
= visit_type_specifier_list(ctx
,
1309 type_specifier_list
, &nested_decl
);
1311 assert(!nested_decl
);
1317 assert(nested_decl
);
1319 if (!node_type_declarator
) {
1320 BT_MOVE(*field_decl
, nested_decl
);
1324 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1325 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1327 node_type_declarator
->u
.type_declarator
.u
.id
;
1329 *field_name
= g_quark_from_string(id
);
1334 BT_MOVE(*field_decl
, nested_decl
);
1337 struct ctf_node
*first
;
1338 _BT_CTF_FIELD_TYPE_INIT(decl
);
1339 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1340 struct bt_list_head
*length
=
1341 &node_type_declarator
->
1342 u
.type_declarator
.u
.nested
.length
;
1344 /* Create array/sequence, pass nested_decl as child */
1345 if (bt_list_empty(length
)) {
1347 "expecting length field reference or value");
1352 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1353 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1358 switch (first
->u
.unary_expression
.type
) {
1359 case UNARY_UNSIGNED_CONSTANT
:
1362 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1364 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1365 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1367 BT_PUT(nested_decl
);
1370 "cannot create array field type");
1375 BT_MOVE(decl
, array_decl
);
1380 /* Lookup unsigned integer definition, create seq. */
1381 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1382 char *length_name
= concatenate_unary_strings(length
);
1389 seq_decl
= bt_ctf_field_type_sequence_create(
1390 nested_decl
, length_name
);
1391 g_free(length_name
);
1392 BT_PUT(nested_decl
);
1395 "cannot create sequence field type");
1400 BT_MOVE(decl
, seq_decl
);
1408 assert(!nested_decl
);
1410 assert(!*field_decl
);
1413 * At this point, we found the next nested declaration.
1414 * We currently own this (and lost the ownership of
1415 * nested_decl in the meantime). Pass this next
1416 * nested declaration as the content of the outer
1417 * container, MOVING its ownership.
1419 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1421 node_type_declarator
->
1422 u
.type_declarator
.u
.nested
.type_declarator
,
1423 &outer_field_decl
, decl
);
1426 assert(!outer_field_decl
);
1431 assert(outer_field_decl
);
1432 BT_MOVE(*field_decl
, outer_field_decl
);
1436 BT_PUT(nested_decl
);
1437 assert(*field_decl
);
1442 BT_PUT(nested_decl
);
1443 BT_PUT(*field_decl
);
1449 int visit_struct_decl_field(struct ctx
*ctx
,
1450 struct bt_ctf_field_type
*struct_decl
,
1451 struct ctf_node
*type_specifier_list
,
1452 struct bt_list_head
*type_declarators
)
1455 struct ctf_node
*iter
;
1456 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1458 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1461 const char *field_name
;
1462 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1464 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1465 &qfield_name
, iter
, &field_decl
, NULL
);
1467 assert(!field_decl
);
1468 _PERROR("%s", "cannot visit type declarator");
1473 field_name
= g_quark_to_string(qfield_name
);
1475 /* Check if field with same name already exists */
1477 bt_ctf_field_type_structure_get_field_type_by_name(
1478 struct_decl
, field_name
);
1480 BT_PUT(efield_decl
);
1481 _PERROR("duplicate field \"%s\" in structure",
1487 /* Add field to structure */
1488 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1489 field_decl
, field_name
);
1492 _PERROR("cannot add field \"%s\" to structure",
1493 g_quark_to_string(qfield_name
));
1507 int visit_variant_decl_field(struct ctx
*ctx
,
1508 struct bt_ctf_field_type
*variant_decl
,
1509 struct ctf_node
*type_specifier_list
,
1510 struct bt_list_head
*type_declarators
)
1513 struct ctf_node
*iter
;
1514 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1516 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1519 const char *field_name
;
1520 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1522 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1523 &qfield_name
, iter
, &field_decl
, NULL
);
1525 assert(!field_decl
);
1527 "cannot visit type declarator");
1532 field_name
= g_quark_to_string(qfield_name
);
1534 /* Check if field with same name already exists */
1536 bt_ctf_field_type_variant_get_field_type_by_name(
1537 variant_decl
, field_name
);
1539 BT_PUT(efield_decl
);
1540 _PERROR("duplicate field \"%s\" in variant",
1546 /* Add field to structure */
1547 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1548 field_decl
, field_name
);
1551 _PERROR("cannot add field \"%s\" to variant",
1552 g_quark_to_string(qfield_name
));
1566 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1567 struct bt_list_head
*type_declarators
)
1571 struct ctf_node
*iter
;
1572 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1574 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1575 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1576 &qidentifier
, iter
, &type_decl
, NULL
);
1578 _PERROR("%s", "cannot visit type declarator");
1583 /* Do not allow typedef and typealias of untagged variants */
1584 if (bt_ctf_field_type_is_variant(type_decl
)) {
1585 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1586 _PERROR("%s", "typedef of untagged variant is not allowed");
1592 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1593 g_quark_to_string(qidentifier
), type_decl
);
1595 _PERROR("cannot register typedef \"%s\"",
1596 g_quark_to_string(qidentifier
));
1608 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1609 struct ctf_node
*alias
)
1613 struct ctf_node
*node
;
1614 GQuark qdummy_field_name
;
1615 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1617 /* Create target type declaration */
1618 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1621 node
= _BT_LIST_FIRST_ENTRY(
1622 &target
->u
.typealias_target
.type_declarators
,
1623 struct ctf_node
, siblings
);
1626 ret
= visit_type_declarator(ctx
,
1627 target
->u
.typealias_target
.type_specifier_list
,
1628 &qdummy_field_name
, node
, &type_decl
, NULL
);
1631 _PERROR("%s", "cannot visit type declarator");
1635 /* Do not allow typedef and typealias of untagged variants */
1636 if (bt_ctf_field_type_is_variant(type_decl
)) {
1637 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1639 "typealias of untagged variant is not allowed");
1646 * The semantic validator does not check whether the target is
1647 * abstract or not (if it has an identifier). Check it here.
1649 if (qdummy_field_name
!= 0) {
1650 _PERROR("%s", "expecting empty identifier");
1655 /* Create alias identifier */
1656 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1657 struct ctf_node
, siblings
);
1658 qalias
= create_typealias_identifier(ctx
,
1659 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1660 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1661 g_quark_to_string(qalias
), type_decl
);
1663 _PERROR("cannot register typealias \"%s\"",
1664 g_quark_to_string(qalias
));
1675 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1676 struct bt_ctf_field_type
*struct_decl
)
1680 switch (entry_node
->type
) {
1682 ret
= visit_typedef(ctx
,
1683 entry_node
->u
._typedef
.type_specifier_list
,
1684 &entry_node
->u
._typedef
.type_declarators
);
1687 "cannot add typedef in \"struct\" declaration");
1691 case NODE_TYPEALIAS
:
1692 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1693 entry_node
->u
.typealias
.alias
);
1696 "cannot add typealias in \"struct\" declaration");
1700 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1702 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1703 entry_node
->u
.struct_or_variant_declaration
.
1704 type_specifier_list
,
1705 &entry_node
->u
.struct_or_variant_declaration
.
1712 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1722 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1723 struct bt_ctf_field_type
*variant_decl
)
1727 switch (entry_node
->type
) {
1729 ret
= visit_typedef(ctx
,
1730 entry_node
->u
._typedef
.type_specifier_list
,
1731 &entry_node
->u
._typedef
.type_declarators
);
1734 "cannot add typedef in \"variant\" declaration");
1738 case NODE_TYPEALIAS
:
1739 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1740 entry_node
->u
.typealias
.alias
);
1743 "cannot add typealias in \"variant\" declaration");
1747 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1749 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1750 entry_node
->u
.struct_or_variant_declaration
.
1751 type_specifier_list
,
1752 &entry_node
->u
.struct_or_variant_declaration
.
1759 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1769 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1770 struct bt_list_head
*decl_list
, int has_body
,
1771 struct bt_list_head
*min_align
,
1772 struct bt_ctf_field_type
**struct_decl
)
1776 *struct_decl
= NULL
;
1778 /* For named struct (without body), lookup in declaration scope */
1780 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1787 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1789 if (!*struct_decl
) {
1790 _PERROR("cannot find \"struct %s\"", name
);
1795 /* Make a copy of it */
1796 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1797 if (!struct_decl_copy
) {
1799 "cannot create copy of structure field type");
1804 BT_MOVE(*struct_decl
, struct_decl_copy
);
1806 struct ctf_node
*entry_node
;
1807 uint64_t min_align_value
= 0;
1810 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1812 estruct_decl
= ctx_decl_scope_lookup_struct(
1813 ctx
->current_scope
, name
, 1);
1815 BT_PUT(estruct_decl
);
1816 _PERROR("\"struct %s\" already declared in local scope",
1823 if (!bt_list_empty(min_align
)) {
1824 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1826 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1831 *struct_decl
= bt_ctf_field_type_structure_create();
1832 if (!*struct_decl
) {
1833 _PERROR("%s", "cannot create structure field type");
1838 if (min_align_value
!= 0) {
1839 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1842 _PERROR("%s", "failed to set structure's minimal alignment");
1847 ret
= ctx_push_scope(ctx
);
1849 _PERROR("%s", "cannot push scope");
1853 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1854 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1865 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1866 name
, *struct_decl
);
1868 _PERROR("cannot register \"struct %s\" in declaration scope",
1878 BT_PUT(*struct_decl
);
1884 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1885 const char *tag
, struct bt_list_head
*decl_list
,
1886 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1889 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1891 *variant_decl
= NULL
;
1893 /* For named variant (without body), lookup in declaration scope */
1895 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1902 untagged_variant_decl
=
1903 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1905 if (!untagged_variant_decl
) {
1906 _PERROR("cannot find \"variant %s\"", name
);
1911 /* Make a copy of it */
1912 variant_decl_copy
= bt_ctf_field_type_copy(
1913 untagged_variant_decl
);
1914 if (!variant_decl_copy
) {
1916 "cannot create copy of variant field type");
1921 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1923 struct ctf_node
*entry_node
;
1926 struct bt_ctf_field_type
*evariant_decl
=
1927 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1930 if (evariant_decl
) {
1931 BT_PUT(evariant_decl
);
1932 _PERROR("\"variant %s\" already declared in local scope",
1939 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1941 if (!untagged_variant_decl
) {
1942 _PERROR("%s", "cannot create variant field type");
1947 ret
= ctx_push_scope(ctx
);
1949 _PERROR("%s", "cannot push scope");
1953 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1954 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1955 untagged_variant_decl
);
1965 ret
= ctx_decl_scope_register_variant(
1966 ctx
->current_scope
, name
,
1967 untagged_variant_decl
);
1969 _PERROR("cannot register \"variant %s\" in declaration scope",
1977 * If tagged, create tagged variant and return; otherwise
1978 * return untagged variant.
1981 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1984 * At this point, we have a fresh untagged variant; nobody
1985 * else owns it. Set its tag now.
1987 ret
= bt_ctf_field_type_variant_set_tag_name(
1988 untagged_variant_decl
, tag
);
1993 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1996 assert(!untagged_variant_decl
);
1997 assert(*variant_decl
);
2002 BT_PUT(untagged_variant_decl
);
2003 BT_PUT(*variant_decl
);
2009 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2010 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2014 struct ctf_node
*iter
;
2015 int64_t start
= 0, end
= 0;
2016 const char *label
= enumerator
->u
.enumerator
.id
;
2017 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2019 bt_list_for_each_entry(iter
, values
, siblings
) {
2022 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2023 _PERROR("wrong unary expression for enumeration label \"%s\"",
2035 switch (iter
->u
.unary_expression
.type
) {
2036 case UNARY_SIGNED_CONSTANT
:
2037 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2039 case UNARY_UNSIGNED_CONSTANT
:
2041 iter
->u
.unary_expression
.u
.unsigned_constant
;
2044 _PERROR("invalid enumeration entry: \"%s\"",
2051 _PERROR("invalid enumeration entry: \"%s\"",
2071 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2074 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2075 label
, (uint64_t) start
, (uint64_t) end
);
2078 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2090 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2091 struct ctf_node
*container_type
,
2092 struct bt_list_head
*enumerator_list
,
2094 struct bt_ctf_field_type
**enum_decl
)
2098 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2102 /* For named enum (without body), lookup in declaration scope */
2104 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2111 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2114 _PERROR("cannot find \"enum %s\"", name
);
2119 /* Make a copy of it */
2120 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2121 if (!enum_decl_copy
) {
2123 "cannot create copy of enumeration field type");
2129 BT_MOVE(*enum_decl
, enum_decl_copy
);
2131 struct ctf_node
*iter
;
2132 int64_t last_value
= 0;
2135 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2137 eenum_decl
= ctx_decl_scope_lookup_enum(
2138 ctx
->current_scope
, name
, 1);
2141 _PERROR("\"enum %s\" already declared in local scope",
2148 if (!container_type
) {
2149 integer_decl
= ctx_decl_scope_lookup_alias(
2150 ctx
->current_scope
, "int", -1);
2151 if (!integer_decl
) {
2152 _PERROR("%s", "cannot find \"int\" type for enumeration");
2157 ret
= visit_type_declarator(ctx
, container_type
,
2158 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2160 assert(!integer_decl
);
2166 assert(integer_decl
);
2168 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2169 _PERROR("%s", "container type for enumeration is not an integer");
2174 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2176 _PERROR("%s", "cannot create enumeration field type");
2181 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2182 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2184 bt_ctf_field_type_integer_get_signed(integer_decl
));
2191 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2199 BT_PUT(integer_decl
);
2204 BT_PUT(integer_decl
);
2211 int visit_type_specifier(struct ctx
*ctx
,
2212 struct ctf_node
*type_specifier_list
,
2213 struct bt_ctf_field_type
**decl
)
2216 GString
*str
= NULL
;
2217 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2220 str
= g_string_new("");
2221 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2226 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2228 _PERROR("cannot find type alias \"%s\"", str
->str
);
2233 /* Make a copy of the type declaration */
2234 decl_copy
= bt_ctf_field_type_copy(*decl
);
2236 _PERROR("%s", "cannot create field type copy");
2241 BT_MOVE(*decl
, decl_copy
);
2242 (void) g_string_free(str
, TRUE
);
2249 (void) g_string_free(str
, TRUE
);
2258 int visit_integer_decl(struct ctx
*ctx
,
2259 struct bt_list_head
*expressions
,
2260 struct bt_ctf_field_type
**integer_decl
)
2265 struct ctf_node
*expression
;
2266 uint64_t alignment
= 0, size
= 0;
2267 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2268 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2269 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2270 enum bt_ctf_byte_order byte_order
=
2271 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2273 *integer_decl
= NULL
;
2275 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2276 struct ctf_node
*left
, *right
;
2278 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2279 struct ctf_node
, siblings
);
2280 right
= _BT_LIST_FIRST_ENTRY(
2281 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2284 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2289 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2290 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2291 _PERROR_DUP_ATTR("signed",
2292 "integer declaration");
2297 signedness
= get_boolean(ctx
->efd
, right
);
2298 if (signedness
< 0) {
2303 _SET(&set
, _INTEGER_SIGNED_SET
);
2304 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2306 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2307 _PERROR_DUP_ATTR("byte_order",
2308 "integer declaration");
2313 byte_order
= get_real_byte_order(ctx
, right
);
2314 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2315 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2320 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2321 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2322 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2323 _PERROR_DUP_ATTR("size",
2324 "integer declaration");
2329 if (right
->u
.unary_expression
.type
!=
2330 UNARY_UNSIGNED_CONSTANT
) {
2331 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2336 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2338 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2341 } else if (size
> 64) {
2342 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2347 _SET(&set
, _INTEGER_SIZE_SET
);
2348 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2350 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2351 _PERROR_DUP_ATTR("align",
2352 "integer declaration");
2357 if (right
->u
.unary_expression
.type
!=
2358 UNARY_UNSIGNED_CONSTANT
) {
2359 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2365 right
->u
.unary_expression
.u
.unsigned_constant
;
2366 if (!is_align_valid(alignment
)) {
2367 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2372 _SET(&set
, _INTEGER_ALIGN_SET
);
2373 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2374 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2375 _PERROR_DUP_ATTR("base", "integer declaration");
2380 switch (right
->u
.unary_expression
.type
) {
2381 case UNARY_UNSIGNED_CONSTANT
:
2383 uint64_t constant
= right
->u
.unary_expression
.
2384 u
.unsigned_constant
;
2388 base
= BT_CTF_INTEGER_BASE_BINARY
;
2391 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2394 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2397 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2400 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2401 right
->u
.unary_expression
.u
.unsigned_constant
);
2409 char *s_right
= concatenate_unary_strings(
2410 &expression
->u
.ctf_expression
.right
);
2412 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2417 if (!strcmp(s_right
, "decimal") ||
2418 !strcmp(s_right
, "dec") ||
2419 !strcmp(s_right
, "d") ||
2420 !strcmp(s_right
, "i") ||
2421 !strcmp(s_right
, "u")) {
2422 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2423 } else if (!strcmp(s_right
, "hexadecimal") ||
2424 !strcmp(s_right
, "hex") ||
2425 !strcmp(s_right
, "x") ||
2426 !strcmp(s_right
, "X") ||
2427 !strcmp(s_right
, "p")) {
2428 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2429 } else if (!strcmp(s_right
, "octal") ||
2430 !strcmp(s_right
, "oct") ||
2431 !strcmp(s_right
, "o")) {
2432 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2433 } else if (!strcmp(s_right
, "binary") ||
2434 !strcmp(s_right
, "b")) {
2435 base
= BT_CTF_INTEGER_BASE_BINARY
;
2437 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2448 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2453 _SET(&set
, _INTEGER_BASE_SET
);
2454 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2458 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2459 _PERROR_DUP_ATTR("encoding",
2460 "integer declaration");
2465 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2466 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2471 s_right
= concatenate_unary_strings(
2472 &expression
->u
.ctf_expression
.right
);
2474 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2479 if (!strcmp(s_right
, "UTF8") ||
2480 !strcmp(s_right
, "utf8") ||
2481 !strcmp(s_right
, "utf-8") ||
2482 !strcmp(s_right
, "UTF-8")) {
2483 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2484 } else if (!strcmp(s_right
, "ASCII") ||
2485 !strcmp(s_right
, "ascii")) {
2486 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2487 } else if (!strcmp(s_right
, "none")) {
2488 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2490 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2498 _SET(&set
, _INTEGER_ENCODING_SET
);
2499 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2500 const char *clock_name
;
2502 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2503 _PERROR_DUP_ATTR("map", "integer declaration");
2508 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2509 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2515 get_map_clock_name_value(
2516 &expression
->u
.ctf_expression
.right
);
2518 char *s_right
= concatenate_unary_strings(
2519 &expression
->u
.ctf_expression
.right
);
2522 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2527 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"",
2529 _SET(&set
, _INTEGER_MAP_SET
);
2534 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2535 ctx
->trace
, clock_name
);
2536 if (!mapped_clock
) {
2537 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"",
2543 _SET(&set
, _INTEGER_MAP_SET
);
2545 _PWARNING("unknown attribute \"%s\" in integer declaration",
2546 left
->u
.unary_expression
.u
.string
);
2550 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2552 "missing \"size\" attribute in integer declaration");
2557 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2558 if (size
% CHAR_BIT
) {
2559 /* Bit-packed alignment */
2562 /* Byte-packed alignment */
2563 alignment
= CHAR_BIT
;
2567 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2568 if (!*integer_decl
) {
2569 _PERROR("%s", "cannot create integer field type");
2574 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2575 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2576 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2577 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2578 (unsigned int) alignment
);
2579 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2583 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2584 *integer_decl
, mapped_clock
);
2585 bt_put(mapped_clock
);
2586 mapped_clock
= NULL
;
2590 _PERROR("%s", "cannot configure integer field type");
2599 bt_put(mapped_clock
);
2602 BT_PUT(*integer_decl
);
2608 int visit_floating_point_number_decl(struct ctx
*ctx
,
2609 struct bt_list_head
*expressions
,
2610 struct bt_ctf_field_type
**float_decl
)
2614 struct ctf_node
*expression
;
2615 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2616 enum bt_ctf_byte_order byte_order
=
2617 bt_ctf_trace_get_native_byte_order(ctx
->trace
);
2621 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2622 struct ctf_node
*left
, *right
;
2624 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2625 struct ctf_node
, siblings
);
2626 right
= _BT_LIST_FIRST_ENTRY(
2627 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2630 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2635 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2636 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2637 _PERROR_DUP_ATTR("byte_order",
2638 "floating point number declaration");
2643 byte_order
= get_real_byte_order(ctx
, right
);
2644 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2645 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2650 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2651 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2653 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2654 _PERROR_DUP_ATTR("exp_dig",
2655 "floating point number declaration");
2660 if (right
->u
.unary_expression
.type
!=
2661 UNARY_UNSIGNED_CONSTANT
) {
2662 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2667 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2668 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2669 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2671 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2672 _PERROR_DUP_ATTR("mant_dig",
2673 "floating point number declaration");
2678 if (right
->u
.unary_expression
.type
!=
2679 UNARY_UNSIGNED_CONSTANT
) {
2680 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2685 mant_dig
= right
->u
.unary_expression
.u
.
2687 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2688 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2690 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2691 _PERROR_DUP_ATTR("align",
2692 "floating point number declaration");
2697 if (right
->u
.unary_expression
.type
!=
2698 UNARY_UNSIGNED_CONSTANT
) {
2699 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2704 alignment
= right
->u
.unary_expression
.u
.
2707 if (!is_align_valid(alignment
)) {
2708 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2713 _SET(&set
, _FLOAT_ALIGN_SET
);
2715 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2716 left
->u
.unary_expression
.u
.string
);
2720 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2721 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2726 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2727 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2732 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2733 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2734 /* Bit-packed alignment */
2737 /* Byte-packed alignment */
2738 alignment
= CHAR_BIT
;
2742 *float_decl
= bt_ctf_field_type_floating_point_create();
2745 "cannot create floating point number field type");
2750 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2751 *float_decl
, exp_dig
);
2752 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2753 *float_decl
, mant_dig
);
2754 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2755 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2758 "cannot configure floating point number field type");
2766 BT_PUT(*float_decl
);
2772 int visit_string_decl(struct ctx
*ctx
,
2773 struct bt_list_head
*expressions
,
2774 struct bt_ctf_field_type
**string_decl
)
2778 struct ctf_node
*expression
;
2779 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2781 *string_decl
= NULL
;
2783 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2784 struct ctf_node
*left
, *right
;
2786 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2787 struct ctf_node
, siblings
);
2788 right
= _BT_LIST_FIRST_ENTRY(
2789 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2792 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2797 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2800 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2801 _PERROR_DUP_ATTR("encoding",
2802 "string declaration");
2807 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2808 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2813 s_right
= concatenate_unary_strings(
2814 &expression
->u
.ctf_expression
.right
);
2816 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2821 if (!strcmp(s_right
, "UTF8") ||
2822 !strcmp(s_right
, "utf8") ||
2823 !strcmp(s_right
, "utf-8") ||
2824 !strcmp(s_right
, "UTF-8")) {
2825 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2826 } else if (!strcmp(s_right
, "ASCII") ||
2827 !strcmp(s_right
, "ascii")) {
2828 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2829 } else if (!strcmp(s_right
, "none")) {
2830 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2832 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2840 _SET(&set
, _STRING_ENCODING_SET
);
2842 _PWARNING("unknown attribute \"%s\" in string declaration",
2843 left
->u
.unary_expression
.u
.string
);
2847 *string_decl
= bt_ctf_field_type_string_create();
2848 if (!*string_decl
) {
2849 _PERROR("%s", "cannot create string field type");
2854 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2856 _PERROR("%s", "cannot configure string field type");
2864 BT_PUT(*string_decl
);
2870 int visit_type_specifier_list(struct ctx
*ctx
,
2871 struct ctf_node
*ts_list
,
2872 struct bt_ctf_field_type
**decl
)
2875 struct ctf_node
*first
, *node
;
2879 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2884 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2885 struct ctf_node
, siblings
);
2886 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2891 node
= first
->u
.type_specifier
.node
;
2893 switch (first
->u
.type_specifier
.type
) {
2894 case TYPESPEC_INTEGER
:
2895 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2902 case TYPESPEC_FLOATING_POINT
:
2903 ret
= visit_floating_point_number_decl(ctx
,
2904 &node
->u
.floating_point
.expressions
, decl
);
2910 case TYPESPEC_STRING
:
2911 ret
= visit_string_decl(ctx
,
2912 &node
->u
.string
.expressions
, decl
);
2918 case TYPESPEC_STRUCT
:
2919 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2920 &node
->u
._struct
.declaration_list
,
2921 node
->u
._struct
.has_body
,
2922 &node
->u
._struct
.min_align
, decl
);
2928 case TYPESPEC_VARIANT
:
2929 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2930 node
->u
.variant
.choice
,
2931 &node
->u
.variant
.declaration_list
,
2932 node
->u
.variant
.has_body
, decl
);
2939 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2940 node
->u
._enum
.container_type
,
2941 &node
->u
._enum
.enumerator_list
,
2942 node
->u
._enum
.has_body
, decl
);
2950 case TYPESPEC_SHORT
:
2953 case TYPESPEC_FLOAT
:
2954 case TYPESPEC_DOUBLE
:
2955 case TYPESPEC_SIGNED
:
2956 case TYPESPEC_UNSIGNED
:
2958 case TYPESPEC_COMPLEX
:
2959 case TYPESPEC_IMAGINARY
:
2960 case TYPESPEC_CONST
:
2961 case TYPESPEC_ID_TYPE
:
2962 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2969 _PERROR("unexpected node type: %d",
2970 (int) first
->u
.type_specifier
.type
);
2986 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2987 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2992 _BT_CTF_FIELD_TYPE_INIT(decl
);
2994 switch (node
->type
) {
2996 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2997 &node
->u
._typedef
.type_declarators
);
3000 "cannot add typedef in \"event\" declaration");
3004 case NODE_TYPEALIAS
:
3005 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3006 node
->u
.typealias
.alias
);
3008 _PERROR("%s", "cannot add typealias in \"event\" declaration");
3012 case NODE_CTF_EXPRESSION
:
3014 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3020 if (!strcmp(left
, "name")) {
3021 /* This is already known at this stage */
3022 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3023 _PERROR_DUP_ATTR("name", "event declaration");
3028 _SET(set
, _EVENT_NAME_SET
);
3029 } else if (!strcmp(left
, "id")) {
3032 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3033 _PERROR_DUP_ATTR("id", "event declaration");
3038 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3040 if (ret
|| id
< 0) {
3041 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3046 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3049 "cannot set event declaration's ID");
3053 _SET(set
, _EVENT_ID_SET
);
3054 } else if (!strcmp(left
, "stream_id")) {
3055 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3056 _PERROR_DUP_ATTR("stream_id",
3057 "event declaration");
3062 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3063 (uint64_t *) stream_id
);
3064 if (ret
|| *stream_id
< 0) {
3065 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3070 _SET(set
, _EVENT_STREAM_ID_SET
);
3071 } else if (!strcmp(left
, "context")) {
3072 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3073 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3078 ret
= visit_type_specifier_list(ctx
,
3079 _BT_LIST_FIRST_ENTRY(
3080 &node
->u
.ctf_expression
.right
,
3081 struct ctf_node
, siblings
),
3084 _PERROR("%s", "cannot create event context declaration");
3089 ret
= bt_ctf_event_class_set_context_type(
3093 _PERROR("%s", "cannot set event's context declaration");
3097 _SET(set
, _EVENT_CONTEXT_SET
);
3098 } else if (!strcmp(left
, "fields")) {
3099 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3100 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3105 ret
= visit_type_specifier_list(ctx
,
3106 _BT_LIST_FIRST_ENTRY(
3107 &node
->u
.ctf_expression
.right
,
3108 struct ctf_node
, siblings
),
3111 _PERROR("%s", "cannot create event payload field type");
3116 ret
= bt_ctf_event_class_set_payload_type(
3120 _PERROR("%s", "cannot set event's payload field type");
3124 _SET(set
, _EVENT_FIELDS_SET
);
3125 } else if (!strcmp(left
, "loglevel")) {
3126 uint64_t loglevel_value
;
3127 const char *loglevel_str
;
3128 struct bt_value
*value_obj
, *str_obj
;
3130 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3131 _PERROR_DUP_ATTR("loglevel",
3132 "event declaration");
3137 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3140 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3144 value_obj
= bt_value_integer_create_init(loglevel_value
);
3146 _PERROR("%s", "cannot allocate memory for loglevel value object");
3150 if (bt_ctf_event_class_set_attribute(event_class
,
3151 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3152 _PERROR("%s", "cannot set loglevel value");
3157 loglevel_str
= print_loglevel(loglevel_value
);
3159 str_obj
= bt_value_string_create_init(loglevel_str
);
3160 if (bt_ctf_event_class_set_attribute(event_class
,
3161 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3162 _PERROR("%s", "cannot set loglevel string");
3170 _SET(set
, _EVENT_LOGLEVEL_SET
);
3171 } else if (!strcmp(left
, "model.emf.uri")) {
3174 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3175 _PERROR_DUP_ATTR("model.emf.uri",
3176 "event declaration");
3181 right
= concatenate_unary_strings(
3182 &node
->u
.ctf_expression
.right
);
3184 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3189 // TODO: FIXME: set model EMF URI here
3192 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3194 _PWARNING("unknown attribute \"%s\" in event declaration",
3220 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3224 struct ctf_node
*iter
;
3225 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3227 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3228 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3232 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3237 if (!strcmp(left
, "name")) {
3238 name
= concatenate_unary_strings(
3239 &iter
->u
.ctf_expression
.right
);
3241 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3263 int reset_event_decl_types(struct ctx
*ctx
,
3264 struct bt_ctf_event_class
*event_class
)
3269 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3271 _PERROR("%s", "cannot set initial NULL event context");
3275 /* Event payload. */
3276 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3278 _PERROR("%s", "cannot set initial NULL event payload");
3286 int reset_stream_decl_types(struct ctx
*ctx
,
3287 struct bt_ctf_stream_class
*stream_class
)
3291 /* Packet context. */
3292 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3294 _PERROR("%s", "cannot set initial empty packet context");
3299 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3301 _PERROR("%s", "cannot set initial empty event header");
3305 /* Event context. */
3306 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3308 _PERROR("%s", "cannot set initial empty stream event context");
3316 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3319 struct bt_ctf_stream_class
*stream_class
;
3321 stream_class
= bt_ctf_stream_class_create(NULL
);
3322 if (!stream_class
) {
3323 _PERROR("%s", "cannot create stream class");
3328 * Set packet context, event header, and event context to NULL to
3329 * override the default ones.
3331 ret
= reset_stream_decl_types(ctx
, stream_class
);
3336 return stream_class
;
3339 BT_PUT(stream_class
);
3345 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3350 struct ctf_node
*iter
;
3351 int64_t stream_id
= -1;
3352 char *event_name
= NULL
;
3353 struct bt_ctf_event_class
*event_class
= NULL
;
3354 struct bt_ctf_event_class
*eevent_class
;
3355 struct bt_ctf_stream_class
*stream_class
= NULL
;
3356 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3357 bool pop_scope
= false;
3359 if (node
->visited
) {
3363 node
->visited
= TRUE
;
3364 event_name
= get_event_decl_name(ctx
, node
);
3367 "missing \"name\" attribute in event declaration");
3372 event_class
= bt_ctf_event_class_create(event_name
);
3375 * Unset context and fields to override the default ones.
3377 ret
= reset_event_decl_types(ctx
, event_class
);
3382 ret
= ctx_push_scope(ctx
);
3384 _PERROR("%s", "cannot push scope");
3390 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3391 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3398 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3400 struct bt_ctf_stream_class
*new_stream_class
;
3401 size_t stream_class_count
=
3402 g_hash_table_size(ctx
->stream_classes
) +
3403 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3406 * Allow missing stream_id if there is only a single
3409 switch (stream_class_count
) {
3411 /* Create implicit stream class if there's none */
3412 new_stream_class
= create_reset_stream_class(ctx
);
3413 if (!new_stream_class
) {
3418 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3420 _PERROR("%s", "cannot set stream class's ID");
3421 BT_PUT(new_stream_class
);
3427 /* Move reference to visitor's context */
3428 g_hash_table_insert(ctx
->stream_classes
,
3429 (gpointer
) stream_id
, new_stream_class
);
3430 new_stream_class
= NULL
;
3433 /* Single stream class: get its ID */
3434 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3435 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3436 stream_id
= (int64_t) keys
->data
;
3439 assert(bt_ctf_trace_get_stream_class_count(
3442 bt_ctf_trace_get_stream_class_by_index(
3444 assert(stream_class
);
3445 stream_id
= bt_ctf_stream_class_get_id(
3447 BT_PUT(stream_class
);
3451 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3457 assert(stream_id
>= 0);
3459 /* We have the stream ID now; get the stream class if found */
3460 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3461 (gpointer
) stream_id
);
3462 bt_get(stream_class
);
3463 if (!stream_class
) {
3464 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3466 if (!stream_class
) {
3467 _PERROR("cannot find stream class with ID %" PRId64
,
3474 assert(stream_class
);
3476 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3477 /* Allow only one event without ID per stream */
3478 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3481 "missing \"id\" field in event declaration");
3487 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3489 _PERROR("%s", "cannot set event's ID");
3494 event_id
= bt_ctf_event_class_get_id(event_class
);
3496 _PERROR("%s", "cannot get event's ID");
3501 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3504 BT_PUT(eevent_class
);
3505 _PERROR("duplicate event with ID %" PRId64
" in same stream", event_id
);
3510 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3511 BT_PUT(event_class
);
3513 _PERROR("%s", "cannot add event class to stream class");
3520 bt_put(event_class
);
3528 bt_put(stream_class
);
3533 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3534 struct bt_ctf_stream_class
*stream_class
, int *set
)
3538 _BT_CTF_FIELD_TYPE_INIT(decl
);
3540 switch (node
->type
) {
3542 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3543 &node
->u
._typedef
.type_declarators
);
3546 "cannot add typedef in \"stream\" declaration");
3550 case NODE_TYPEALIAS
:
3551 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3552 node
->u
.typealias
.alias
);
3554 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3558 case NODE_CTF_EXPRESSION
:
3560 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3566 if (!strcmp(left
, "id")) {
3570 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3571 _PERROR_DUP_ATTR("id", "stream declaration");
3576 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3578 if (ret
|| id
< 0) {
3579 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3584 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3587 _PERROR("duplicate stream with ID %" PRId64
,
3593 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3596 "cannot set stream class's ID");
3600 _SET(set
, _STREAM_ID_SET
);
3601 } else if (!strcmp(left
, "event.header")) {
3602 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3603 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3608 ret
= visit_type_specifier_list(ctx
,
3609 _BT_LIST_FIRST_ENTRY(
3610 &node
->u
.ctf_expression
.right
,
3611 struct ctf_node
, siblings
),
3614 _PERROR("%s", "cannot create event header field type");
3620 ret
= bt_ctf_stream_class_set_event_header_type(
3621 stream_class
, decl
);
3624 _PERROR("%s", "cannot set stream's event header field type");
3628 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3629 } else if (!strcmp(left
, "event.context")) {
3630 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3631 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3636 ret
= visit_type_specifier_list(ctx
,
3637 _BT_LIST_FIRST_ENTRY(
3638 &node
->u
.ctf_expression
.right
,
3639 struct ctf_node
, siblings
),
3642 _PERROR("%s", "cannot create stream event context field type");
3648 ret
= bt_ctf_stream_class_set_event_context_type(
3649 stream_class
, decl
);
3652 _PERROR("%s", "cannot set stream's event context field type");
3656 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3657 } else if (!strcmp(left
, "packet.context")) {
3658 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3659 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3664 ret
= visit_type_specifier_list(ctx
,
3665 _BT_LIST_FIRST_ENTRY(
3666 &node
->u
.ctf_expression
.right
,
3667 struct ctf_node
, siblings
),
3670 _PERROR("%s", "cannot create packet context field type");
3676 ret
= bt_ctf_stream_class_set_packet_context_type(
3677 stream_class
, decl
);
3680 _PERROR("%s", "cannot set stream's packet context field type");
3684 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3686 _PWARNING("unknown attribute \"%s\" in stream declaration",
3710 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3715 struct ctf_node
*iter
;
3716 struct bt_ctf_stream_class
*stream_class
= NULL
;
3717 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3718 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3720 if (node
->visited
) {
3724 node
->visited
= TRUE
;
3725 stream_class
= create_reset_stream_class(ctx
);
3726 if (!stream_class
) {
3731 ret
= ctx_push_scope(ctx
);
3733 _PERROR("%s", "cannot push scope");
3737 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3738 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3747 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3748 /* Check that packet header has stream_id field */
3749 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3750 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3752 packet_header_decl
=
3753 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3754 if (!packet_header_decl
) {
3756 "cannot get trace packet header field type");
3761 bt_ctf_field_type_structure_get_field_type_by_name(
3762 packet_header_decl
, "stream_id");
3763 BT_PUT(packet_header_decl
);
3764 if (!stream_id_decl
) {
3765 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3769 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3770 BT_PUT(stream_id_decl
);
3771 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3775 BT_PUT(stream_id_decl
);
3777 /* Allow only _one_ ID-less stream */
3778 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3780 "missing \"id\" field in stream declaration");
3785 /* Automatic ID: 0 */
3786 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3789 id
= bt_ctf_stream_class_get_id(stream_class
);
3791 _PERROR("wrong stream ID: %" PRId64
, id
);
3797 * Make sure that this stream class's ID is currently unique in
3800 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
)) {
3801 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3806 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3808 if (existing_stream_class
) {
3809 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3814 /* Move reference to visitor's context */
3815 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3817 stream_class
= NULL
;
3821 bt_put(stream_class
);
3824 bt_put(existing_stream_class
);
3829 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3833 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3835 switch (node
->type
) {
3837 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3838 &node
->u
._typedef
.type_declarators
);
3841 "cannot add typedef in \"trace\" declaration");
3845 case NODE_TYPEALIAS
:
3846 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3847 node
->u
.typealias
.alias
);
3850 "cannot add typealias in \"trace\" declaration");
3854 case NODE_CTF_EXPRESSION
:
3856 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3862 if (!strcmp(left
, "major")) {
3863 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3864 _PERROR_DUP_ATTR("major", "trace declaration");
3869 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3872 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3877 _SET(set
, _TRACE_MAJOR_SET
);
3878 } else if (!strcmp(left
, "minor")) {
3879 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3880 _PERROR_DUP_ATTR("minor", "trace declaration");
3885 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3888 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3893 _SET(set
, _TRACE_MINOR_SET
);
3894 } else if (!strcmp(left
, "uuid")) {
3895 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3896 _PERROR_DUP_ATTR("uuid", "trace declaration");
3901 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3905 "invalid trace declaration's UUID");
3909 ret
= bt_ctf_trace_set_uuid(ctx
->trace
, ctx
->trace_uuid
);
3912 "cannot set trace's UUID");
3916 _SET(set
, _TRACE_UUID_SET
);
3917 } else if (!strcmp(left
, "byte_order")) {
3918 /* Native byte order is already known at this stage */
3919 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3920 _PERROR_DUP_ATTR("byte_order",
3921 "trace declaration");
3926 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3927 } else if (!strcmp(left
, "packet.header")) {
3928 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3929 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3934 ret
= visit_type_specifier_list(ctx
,
3935 _BT_LIST_FIRST_ENTRY(
3936 &node
->u
.ctf_expression
.right
,
3937 struct ctf_node
, siblings
),
3938 &packet_header_decl
);
3940 _PERROR("%s", "cannot create packet header field type");
3944 assert(packet_header_decl
);
3945 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3946 packet_header_decl
);
3947 BT_PUT(packet_header_decl
);
3949 _PERROR("%s", "cannot set trace's packet header field type");
3953 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3955 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3963 _PERROR("%s", "unknown expression in trace declaration");
3972 BT_PUT(packet_header_decl
);
3978 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3982 struct ctf_node
*iter
;
3983 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3985 if (node
->visited
) {
3989 node
->visited
= TRUE
;
3991 if (ctx
->is_trace_visited
) {
3992 _PERROR("%s", "duplicate \"trace\" block");
3997 ret
= ctx_push_scope(ctx
);
3999 _PERROR("%s", "cannot push scope");
4003 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4004 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4013 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4015 "missing \"major\" attribute in trace declaration");
4020 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4022 "missing \"minor\" attribute in trace declaration");
4027 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4028 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4033 ctx
->is_trace_visited
= TRUE
;
4043 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4047 struct ctf_node
*entry_node
;
4048 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4050 if (node
->visited
) {
4054 node
->visited
= TRUE
;
4056 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4057 struct bt_list_head
*right_head
=
4058 &entry_node
->u
.ctf_expression
.right
;
4060 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4061 _PERROR("%s", "wrong expression in environment entry");
4066 left
= concatenate_unary_strings(
4067 &entry_node
->u
.ctf_expression
.left
);
4069 _PERROR("%s", "cannot get environment entry name");
4074 if (is_unary_string(right_head
)) {
4075 char *right
= concatenate_unary_strings(right_head
);
4078 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4084 if (strcmp(left
, "tracer_name") == 0) {
4085 if (strncmp(right
, "lttng", 5) == 0) {
4090 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4091 ret
= bt_ctf_trace_set_environment_field_string(
4092 ctx
->trace
, left
, right
);
4096 _PERROR("environment: cannot add entry \"%s\" to trace",
4100 } else if (is_unary_unsigned(right_head
) ||
4101 is_unary_signed(right_head
)) {
4104 if (is_unary_unsigned(right_head
)) {
4105 ret
= get_unary_unsigned(right_head
,
4108 ret
= get_unary_signed(right_head
, &v
);
4111 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4117 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4118 ret
= bt_ctf_trace_set_environment_field_integer(
4119 ctx
->trace
, left
, v
);
4121 _PERROR("environment: cannot add entry \"%s\" to trace",
4126 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4144 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4149 struct ctf_node
*node
;
4150 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4152 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4153 if (node
->type
== NODE_CTF_EXPRESSION
) {
4154 struct ctf_node
*right_node
;
4156 left
= concatenate_unary_strings(
4157 &node
->u
.ctf_expression
.left
);
4163 if (!strcmp(left
, "byte_order")) {
4164 enum bt_ctf_byte_order bo
;
4166 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4167 _PERROR_DUP_ATTR("byte_order",
4168 "trace declaration");
4173 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4174 right_node
= _BT_LIST_FIRST_ENTRY(
4175 &node
->u
.ctf_expression
.right
,
4176 struct ctf_node
, siblings
);
4177 bo
= byte_order_from_unary_expr(ctx
->efd
,
4179 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4180 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4183 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4184 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4190 ret
= bt_ctf_trace_set_native_byte_order(
4193 _PERROR("cannot set trace's byte order (%d)",
4204 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4205 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4219 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4220 struct bt_ctf_clock_class
*clock
, int *set
)
4225 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4230 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4236 if (!strcmp(left
, "name")) {
4239 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4240 _PERROR_DUP_ATTR("name", "clock class declaration");
4245 right
= concatenate_unary_strings(
4246 &entry_node
->u
.ctf_expression
.right
);
4248 _PERROR("%s", "unexpected unary expression for clock class declaration's \"name\" attribute");
4253 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4255 _PERROR("%s", "cannot set clock class's name");
4261 _SET(set
, _CLOCK_NAME_SET
);
4262 } else if (!strcmp(left
, "uuid")) {
4263 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4265 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4266 _PERROR_DUP_ATTR("uuid", "clock class declaration");
4271 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4273 _PERROR("%s", "invalid clock class UUID");
4277 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4279 _PERROR("%s", "cannot set clock class's UUID");
4283 _SET(set
, _CLOCK_UUID_SET
);
4284 } else if (!strcmp(left
, "description")) {
4287 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4288 _PERROR_DUP_ATTR("description", "clock class declaration");
4293 right
= concatenate_unary_strings(
4294 &entry_node
->u
.ctf_expression
.right
);
4296 _PERROR("%s", "unexpected unary expression for clock class's \"description\" attribute");
4301 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4303 _PERROR("%s", "cannot set clock class's description");
4309 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4310 } else if (!strcmp(left
, "freq")) {
4313 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4314 _PERROR_DUP_ATTR("freq", "clock class declaration");
4319 ret
= get_unary_unsigned(
4320 &entry_node
->u
.ctf_expression
.right
, &freq
);
4322 _PERROR("%s", "unexpected unary expression for clock class declaration's \"freq\" attribute");
4327 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4329 _PERROR("%s", "cannot set clock class's frequency");
4333 _SET(set
, _CLOCK_FREQ_SET
);
4334 } else if (!strcmp(left
, "precision")) {
4337 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4338 _PERROR_DUP_ATTR("precision", "clock class declaration");
4343 ret
= get_unary_unsigned(
4344 &entry_node
->u
.ctf_expression
.right
, &precision
);
4346 _PERROR("%s", "unexpected unary expression for clock class declaration's \"precision\" attribute");
4351 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4353 _PERROR("%s", "cannot set clock class's precision");
4357 _SET(set
, _CLOCK_PRECISION_SET
);
4358 } else if (!strcmp(left
, "offset_s")) {
4361 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4362 _PERROR_DUP_ATTR("offset_s", "clock class declaration");
4367 ret
= get_unary_unsigned(
4368 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4370 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset_s\" attribute");
4375 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4377 _PERROR("%s", "cannot set clock class's offset in seconds");
4381 _SET(set
, _CLOCK_OFFSET_S_SET
);
4382 } else if (!strcmp(left
, "offset")) {
4385 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4386 _PERROR_DUP_ATTR("offset", "clock class declaration");
4391 ret
= get_unary_unsigned(
4392 &entry_node
->u
.ctf_expression
.right
, &offset
);
4394 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset\" attribute");
4399 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4401 _PERROR("%s", "cannot set clock class's offset in cycles");
4405 _SET(set
, _CLOCK_OFFSET_SET
);
4406 } else if (!strcmp(left
, "absolute")) {
4407 struct ctf_node
*right
;
4409 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4410 _PERROR_DUP_ATTR("absolute", "clock class declaration");
4415 right
= _BT_LIST_FIRST_ENTRY(
4416 &entry_node
->u
.ctf_expression
.right
,
4417 struct ctf_node
, siblings
);
4418 ret
= get_boolean(ctx
->efd
, right
);
4420 _PERROR("%s", "unexpected unary expression for clock class declaration's \"absolute\" attribute");
4425 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4427 _PERROR("%s", "cannot set clock class's absolute option");
4431 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4433 _PWARNING("unknown attribute \"%s\" in clock class declaration",
4449 uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4454 if (frequency
== 1000000000ULL) {
4457 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4464 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4468 int64_t offset_cycles
;
4470 freq
= bt_ctf_clock_class_get_frequency(clock
);
4471 if (freq
== -1ULL) {
4472 _PERROR("%s", "cannot get clock class frequency");
4477 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4479 _PERROR("%s", "cannot get offset in cycles");
4484 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4485 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4492 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4496 struct bt_ctf_clock_class
*clock
;
4497 struct ctf_node
*entry_node
;
4498 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4499 const char *clock_class_name
;
4501 if (clock_node
->visited
) {
4505 clock_node
->visited
= TRUE
;
4506 clock
= bt_ctf_clock_class_create(NULL
);
4508 _PERROR("%s", "cannot create clock");
4513 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4514 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4520 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4522 "missing \"name\" attribute in clock class declaration");
4527 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4528 assert(clock_class_name
);
4529 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4531 * Old versions of LTTng forgot to set its clock class
4532 * as absolute, even if it is. This is important because
4533 * it's a condition to be able to sort notifications
4534 * from different sources.
4536 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4538 _PERROR("%s", "cannot set clock class's absolute option");
4543 ret
= apply_clock_class_offset(ctx
, clock
);
4545 _PERROR("%s", "cannot apply clock class offset ");
4549 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4551 _PERROR("%s", "cannot add clock class to trace");
4562 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4566 if (root_decl_node
->visited
) {
4570 root_decl_node
->visited
= TRUE
;
4572 switch (root_decl_node
->type
) {
4574 ret
= visit_typedef(ctx
,
4575 root_decl_node
->u
._typedef
.type_specifier_list
,
4576 &root_decl_node
->u
._typedef
.type_declarators
);
4578 _PERROR("%s", "cannot add typedef in root scope");
4582 case NODE_TYPEALIAS
:
4583 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4584 root_decl_node
->u
.typealias
.alias
);
4586 _PERROR("%s", "cannot add typealias in root scope");
4590 case NODE_TYPE_SPECIFIER_LIST
:
4592 _BT_CTF_FIELD_TYPE_INIT(decl
);
4595 * Just add the type specifier to the root
4596 * declaration scope. Put local reference.
4598 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4617 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4620 GHashTableIter iter
;
4621 gpointer key
, stream_class
;
4623 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4625 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4626 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4629 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4630 _PERROR("cannot add stream class %" PRId64
" to trace",
4636 g_hash_table_remove_all(ctx
->stream_classes
);
4643 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(FILE *efd
,
4644 uint64_t clock_class_offset_ns
)
4647 struct ctx
*ctx
= NULL
;
4648 struct bt_ctf_trace
*trace
;
4650 trace
= bt_ctf_trace_create();
4652 _FPERROR(efd
, "%s", "cannot create trace");
4656 /* Set packet header to NULL to override the default one */
4657 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
4660 "cannot set initial, empty packet header structure");
4664 /* Create visitor's context */
4665 ctx
= ctx_create(trace
, efd
, clock_class_offset_ns
);
4667 _FPERROR(efd
, "%s", "cannot create visitor context");
4680 return (void *) ctx
;
4684 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4686 ctx_destroy((void *) visitor
);
4690 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
4691 struct ctf_visitor_generate_ir
*visitor
)
4693 struct ctx
*ctx
= (void *) visitor
;
4697 return bt_get(ctx
->trace
);
4701 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4702 struct ctf_node
*node
)
4705 struct ctx
*ctx
= (void *) visitor
;
4707 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4709 switch (node
->type
) {
4712 struct ctf_node
*iter
;
4713 int got_trace_decl
= FALSE
;
4714 int found_callsite
= FALSE
;
4717 * The first thing we need is the native byte order of
4718 * the trace block, because early type aliases can have
4719 * a `byte_order` attribute set to `native`. If we don't
4720 * have the native byte order yet, and we don't have any
4721 * trace block yet, then fail with EINCOMPLETE.
4723 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4724 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4725 if (got_trace_decl
) {
4726 _PERROR("%s", "duplicate trace declaration");
4731 ret
= set_trace_byte_order(ctx
, iter
);
4733 _PERROR("cannot set trace's native byte order (%d)",
4738 got_trace_decl
= TRUE
;
4741 if (!got_trace_decl
) {
4747 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
4748 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
4749 assert(ctx
->current_scope
&&
4750 ctx
->current_scope
->parent_scope
== NULL
);
4753 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4754 ret
= visit_env(ctx
, iter
);
4756 _PERROR("error while visiting environment block (%d)",
4762 assert(ctx
->current_scope
&&
4763 ctx
->current_scope
->parent_scope
== NULL
);
4766 * Visit clock blocks.
4768 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4769 ret
= visit_clock_decl(ctx
, iter
);
4771 _PERROR("error while visiting clock class declaration (%d)",
4777 assert(ctx
->current_scope
&&
4778 ctx
->current_scope
->parent_scope
== NULL
);
4781 * Visit root declarations next, as they can be used by any
4784 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4786 ret
= visit_root_decl(ctx
, iter
);
4788 _PERROR("error while visiting root declaration (%d)",
4794 assert(ctx
->current_scope
&&
4795 ctx
->current_scope
->parent_scope
== NULL
);
4797 /* Callsite blocks are not supported */
4798 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4799 found_callsite
= TRUE
;
4803 assert(ctx
->current_scope
&&
4804 ctx
->current_scope
->parent_scope
== NULL
);
4806 if (found_callsite
) {
4807 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4811 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4812 ret
= visit_trace_decl(ctx
, iter
);
4814 _PERROR("%s", "error while visiting trace declaration");
4819 assert(ctx
->current_scope
&&
4820 ctx
->current_scope
->parent_scope
== NULL
);
4823 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4824 ret
= visit_stream_decl(ctx
, iter
);
4826 _PERROR("%s", "error while visiting stream declaration");
4831 assert(ctx
->current_scope
&&
4832 ctx
->current_scope
->parent_scope
== NULL
);
4835 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4836 ret
= visit_event_decl(ctx
, iter
);
4838 _PERROR("%s", "error while visiting event declaration");
4843 assert(ctx
->current_scope
&&
4844 ctx
->current_scope
->parent_scope
== NULL
);
4848 _PERROR("cannot decode node type: %d", (int) node
->type
);
4853 /* Move decoded stream classes to trace, if any */
4854 ret
= move_ctx_stream_classes_to_trace(ctx
);
4856 _PERROR("%s", "cannot move stream classes to trace");