2 * ctf-visitor-generate-ir.c
4 * Common Trace Format metadata visitor (generates CTF IR objects).
6 * Based on older ctf-visitor-generate-io-struct.c.
8 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Copyright 2015-2016 - Philippe Proulx <philippe.proulx@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include <babeltrace/compat/uuid-internal.h>
40 #include <babeltrace/endian-internal.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/ctf-ir/trace.h>
43 #include <babeltrace/ctf-ir/stream-class.h>
44 #include <babeltrace/ctf-ir/event.h>
45 #include <babeltrace/ctf-ir/event-class.h>
46 #include <babeltrace/ctf-ir/field-types.h>
47 #include <babeltrace/ctf-ir/field-types-internal.h>
48 #include <babeltrace/ctf-ir/clock-class.h>
54 /* Bit value (left shift) */
55 #define _BV(_val) (1 << (_val))
57 /* Bit is set in a set of bits */
58 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
60 /* Set bit in a set of bits */
61 #define _SET(_set, _mask) (*(_set) |= (_mask))
63 /* Bits for verifying existing attributes in various declarations */
65 _CLOCK_NAME_SET
= _BV(0),
66 _CLOCK_UUID_SET
= _BV(1),
67 _CLOCK_FREQ_SET
= _BV(2),
68 _CLOCK_PRECISION_SET
= _BV(3),
69 _CLOCK_OFFSET_S_SET
= _BV(4),
70 _CLOCK_OFFSET_SET
= _BV(5),
71 _CLOCK_ABSOLUTE_SET
= _BV(6),
72 _CLOCK_DESCRIPTION_SET
= _BV(7),
76 _INTEGER_ALIGN_SET
= _BV(0),
77 _INTEGER_SIZE_SET
= _BV(1),
78 _INTEGER_BASE_SET
= _BV(2),
79 _INTEGER_ENCODING_SET
= _BV(3),
80 _INTEGER_BYTE_ORDER_SET
= _BV(4),
81 _INTEGER_SIGNED_SET
= _BV(5),
82 _INTEGER_MAP_SET
= _BV(6),
86 _FLOAT_ALIGN_SET
= _BV(0),
87 _FLOAT_MANT_DIG_SET
= _BV(1),
88 _FLOAT_EXP_DIG_SET
= _BV(2),
89 _FLOAT_BYTE_ORDER_SET
= _BV(3),
93 _STRING_ENCODING_SET
= _BV(0),
97 _TRACE_MINOR_SET
= _BV(0),
98 _TRACE_MAJOR_SET
= _BV(1),
99 _TRACE_BYTE_ORDER_SET
= _BV(2),
100 _TRACE_UUID_SET
= _BV(3),
101 _TRACE_PACKET_HEADER_SET
= _BV(4),
105 _STREAM_ID_SET
= _BV(0),
106 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
107 _STREAM_EVENT_HEADER_SET
= _BV(2),
108 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
112 _EVENT_NAME_SET
= _BV(0),
113 _EVENT_ID_SET
= _BV(1),
114 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
115 _EVENT_STREAM_ID_SET
= _BV(3),
116 _EVENT_LOGLEVEL_SET
= _BV(4),
117 _EVENT_CONTEXT_SET
= _BV(5),
118 _EVENT_FIELDS_SET
= _BV(6),
126 LOGLEVEL_WARNING
= 4,
129 LOGLEVEL_DEBUG_SYSTEM
= 7,
130 LOGLEVEL_DEBUG_PROGRAM
= 8,
131 LOGLEVEL_DEBUG_PROCESS
= 9,
132 LOGLEVEL_DEBUG_MODULE
= 10,
133 LOGLEVEL_DEBUG_UNIT
= 11,
134 LOGLEVEL_DEBUG_FUNCTION
= 12,
135 LOGLEVEL_DEBUG_LINE
= 13,
140 /* Prefixes of type aliases */
141 #define _PREFIX_ALIAS 'a'
142 #define _PREFIX_ENUM 'e'
143 #define _PREFIX_STRUCT 's'
144 #define _PREFIX_VARIANT 'v'
146 /* First entry in a BT list */
147 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
148 bt_list_entry((_ptr)->next, _type, _member)
150 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
152 /* Error printing wrappers */
153 #define _PERROR(_fmt, ...) \
155 if (!ctx->efd) break; \
156 fprintf(ctx->efd, "[error] %s: " _fmt "\n", \
157 __func__, __VA_ARGS__); \
160 #define _PWARNING(_fmt, ...) \
162 if (!ctx->efd) break; \
163 fprintf(ctx->efd, "[warning] %s: " _fmt "\n", \
164 __func__, __VA_ARGS__); \
167 #define _FPERROR(_stream, _fmt, ...) \
169 if (!_stream) break; \
170 fprintf(_stream, "[error] %s: " _fmt "\n", \
171 __func__, __VA_ARGS__); \
174 #define _FPWARNING(_stream, _fmt, ...) \
176 if (!_stream) break; \
177 fprintf(_stream, "[warning] %s: " _fmt "\n", \
178 __func__, __VA_ARGS__); \
181 #define _PERROR_DUP_ATTR(_attr, _entity) \
183 if (!ctx->efd) break; \
185 "[error] %s: duplicate attribute \"" \
186 _attr "\" in " _entity "\n", __func__); \
190 * Declaration scope of a visitor context. This represents a TSDL
191 * lexical scope, so that aliases and named structures, variants,
192 * and enumerations may be registered and looked up hierarchically.
194 struct ctx_decl_scope
{
196 * Alias name to field type.
198 * GQuark -> struct bt_ctf_field_type *
200 GHashTable
*decl_map
;
202 /* Parent scope; NULL if this is the root declaration scope */
203 struct ctx_decl_scope
*parent_scope
;
207 * Visitor context (private).
210 /* Trace being filled (owned by this) */
211 struct bt_ctf_trace
*trace
;
213 /* Error stream to use during visit */
216 /* Current declaration scope (top of the stack) */
217 struct ctx_decl_scope
*current_scope
;
219 /* 1 if trace declaration is visited */
220 int is_trace_visited
;
222 /* 1 if this is an LTTng trace */
225 /* Offset (ns) to apply to clock classes on creation */
226 uint64_t clock_class_offset_ns
;
228 /* Trace attributes */
229 enum bt_ctf_byte_order trace_bo
;
230 uint64_t trace_major
;
231 uint64_t trace_minor
;
232 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
235 * Stream IDs to stream classes.
237 * int64_t -> struct bt_ctf_stream_class *
239 GHashTable
*stream_classes
;
245 struct ctf_visitor_generate_ir
{ };
248 const char *loglevel_str
[] = {
249 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
250 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
251 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
252 [ LOGLEVEL_ERR
] = "TRACE_ERR",
253 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
254 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
255 [ LOGLEVEL_INFO
] = "TRACE_INFO",
256 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
257 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
258 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
259 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
260 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
261 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
262 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
263 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
267 const char *print_loglevel(int64_t value
)
272 if (value
>= _NR_LOGLEVELS
) {
273 return "<<UNKNOWN>>";
275 return loglevel_str
[value
];
279 * Creates a new declaration scope.
281 * @param par_scope Parent scope (NULL if creating a root scope)
282 * @returns New declaration scope, or NULL on error
285 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
287 struct ctx_decl_scope
*scope
;
289 scope
= g_new(struct ctx_decl_scope
, 1);
294 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
295 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
296 scope
->parent_scope
= par_scope
;
303 * Destroys a declaration scope.
305 * This function does not destroy the parent scope.
307 * @param scope Scope to destroy
310 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
316 g_hash_table_destroy(scope
->decl_map
);
324 * Returns the GQuark of a prefixed alias.
326 * @param prefix Prefix character
328 * @returns Associated GQuark, or 0 on error
331 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
337 /* Prefix character + original string + '\0' */
338 char *prname
= g_new(char, strlen(name
) + 2);
343 sprintf(prname
, "%c%s", prefix
, name
);
344 qname
= g_quark_from_string(prname
);
352 * Looks up a prefixed type alias within a declaration scope.
354 * @param scope Declaration scope
355 * @param prefix Prefix character
356 * @param name Alias name
357 * @param level Number of levels to dig (-1 means infinite)
358 * @returns Declaration, or NULL if not found
361 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
362 struct ctx_decl_scope
*scope
, char prefix
,
363 const char *name
, int levels
)
367 _BT_CTF_FIELD_TYPE_INIT(decl
);
368 struct ctx_decl_scope
*cur_scope
= scope
;
372 qname
= get_prefixed_named_quark(prefix
, name
);
381 while (cur_scope
&& cur_levels
< levels
) {
382 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
383 (gconstpointer
) (unsigned long) qname
);
385 /* Caller's reference */
390 cur_scope
= cur_scope
->parent_scope
;
401 * Looks up a type alias within a declaration scope.
403 * @param scope Declaration scope
404 * @param name Alias name
405 * @param level Number of levels to dig (-1 means infinite)
406 * @returns Declaration, or NULL if not found
409 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
410 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
412 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
417 * Looks up an enumeration within a declaration scope.
419 * @param scope Declaration scope
420 * @param name Enumeration name
421 * @param level Number of levels to dig (-1 means infinite)
422 * @returns Declaration, or NULL if not found
425 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
426 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
428 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
433 * Looks up a structure within a declaration scope.
435 * @param scope Declaration scope
436 * @param name Structure name
437 * @param level Number of levels to dig (-1 means infinite)
438 * @returns Declaration, or NULL if not found
441 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
442 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
444 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
449 * Looks up a variant within a declaration scope.
451 * @param scope Declaration scope
452 * @param name Variant name
453 * @param level Number of levels to dig (-1 means infinite)
454 * @returns Declaration, or NULL if not found
457 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
458 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
460 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
465 * Registers a prefixed type alias within a declaration scope.
467 * @param scope Declaration scope
468 * @param prefix Prefix character
469 * @param name Alias name (non-NULL)
470 * @param decl Declaration to register
471 * @returns 0 if registration went okay, negative value otherwise
474 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
475 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
479 _BT_CTF_FIELD_TYPE_INIT(edecl
);
484 qname
= get_prefixed_named_quark(prefix
, name
);
490 /* Make sure alias does not exist in local scope */
491 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
498 g_hash_table_insert(scope
->decl_map
,
499 (gpointer
) (unsigned long) qname
, decl
);
501 /* Hash table's reference */
511 * Registers a type alias within a declaration scope.
513 * @param scope Declaration scope
514 * @param name Alias name (non-NULL)
515 * @param decl Declaration to register
516 * @returns 0 if registration went okay, negative value otherwise
519 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
520 const char *name
, struct bt_ctf_field_type
*decl
)
522 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
527 * Registers an enumeration declaration within a declaration scope.
529 * @param scope Declaration scope
530 * @param name Enumeration name (non-NULL)
531 * @param decl Enumeration declaration to register
532 * @returns 0 if registration went okay, negative value otherwise
535 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
536 const char *name
, struct bt_ctf_field_type
*decl
)
538 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
543 * Registers a structure declaration within a declaration scope.
545 * @param scope Declaration scope
546 * @param name Structure name (non-NULL)
547 * @param decl Structure declaration to register
548 * @returns 0 if registration went okay, negative value otherwise
551 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
552 const char *name
, struct bt_ctf_field_type
*decl
)
554 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
559 * Registers a variant declaration within a declaration scope.
561 * @param scope Declaration scope
562 * @param name Variant name (non-NULL)
563 * @param decl Variant declaration to register
564 * @returns 0 if registration went okay, negative value otherwise
567 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
568 const char *name
, struct bt_ctf_field_type
*decl
)
570 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
575 * Creates a new visitor context.
577 * @param trace Associated trace
578 * @param efd Error stream
579 * @returns New visitor context, or NULL on error
582 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
,
583 uint64_t clock_class_offset_ns
)
585 struct ctx
*ctx
= NULL
;
586 struct ctx_decl_scope
*scope
= NULL
;
588 ctx
= g_new0(struct ctx
, 1);
593 /* Root declaration scope */
594 scope
= ctx_decl_scope_create(NULL
);
599 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
600 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
601 if (!ctx
->stream_classes
) {
607 ctx
->current_scope
= scope
;
608 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
609 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
614 ctx_decl_scope_destroy(scope
);
619 * Destroys a visitor context.
621 * @param ctx Visitor context to destroy
624 void ctx_destroy(struct ctx
*ctx
)
626 struct ctx_decl_scope
*scope
;
628 * Destroy all scopes, from current one to the root scope.
635 scope
= ctx
->current_scope
;
638 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
640 ctx_decl_scope_destroy(scope
);
641 scope
= parent_scope
;
645 g_hash_table_destroy(ctx
->stream_classes
);
653 * Pushes a new declaration scope on top of a visitor context's
654 * declaration scope stack.
656 * @param ctx Visitor context
657 * @returns 0 on success, or a negative value on error
660 int ctx_push_scope(struct ctx
*ctx
)
663 struct ctx_decl_scope
*new_scope
;
666 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
672 ctx
->current_scope
= new_scope
;
679 void ctx_pop_scope(struct ctx
*ctx
)
681 struct ctx_decl_scope
*parent_scope
= NULL
;
685 if (!ctx
->current_scope
) {
689 parent_scope
= ctx
->current_scope
->parent_scope
;
690 ctx_decl_scope_destroy(ctx
->current_scope
);
691 ctx
->current_scope
= parent_scope
;
698 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
699 struct bt_ctf_field_type
**decl
);
702 int is_unary_string(struct bt_list_head
*head
)
705 struct ctf_node
*node
;
707 bt_list_for_each_entry(node
, head
, siblings
) {
708 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
712 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
721 char *concatenate_unary_strings(struct bt_list_head
*head
)
725 struct ctf_node
*node
;
727 str
= g_string_new(NULL
);
729 bt_list_for_each_entry(node
, head
, siblings
) {
733 node
->type
!= NODE_UNARY_EXPRESSION
||
734 node
->u
.unary_expression
.type
!= UNARY_STRING
||
737 node
->u
.unary_expression
.link
!=
745 switch (node
->u
.unary_expression
.link
) {
747 g_string_append(str
, ".");
749 case UNARY_ARROWLINK
:
750 g_string_append(str
, "->");
752 case UNARY_DOTDOTDOT
:
753 g_string_append(str
, "...");
759 src_string
= node
->u
.unary_expression
.u
.string
;
760 g_string_append(str
, src_string
);
764 /* Destroys the container, returns the underlying string */
765 return g_string_free(str
, FALSE
);
768 /* This always returns NULL */
769 return g_string_free(str
, TRUE
);
773 const char *get_map_clock_name_value(struct bt_list_head
*head
)
776 struct ctf_node
*node
;
777 const char *name
= NULL
;
779 bt_list_for_each_entry(node
, head
, siblings
) {
781 int uexpr_type
= node
->u
.unary_expression
.type
;
782 int uexpr_link
= node
->u
.unary_expression
.link
;
783 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
784 uexpr_type
!= UNARY_STRING
||
785 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
790 /* Needs to be chained with . */
791 switch (node
->u
.unary_expression
.link
) {
794 case UNARY_ARROWLINK
:
795 case UNARY_DOTDOTDOT
:
801 src_string
= node
->u
.unary_expression
.u
.string
;
805 if (strcmp("clock", src_string
)) {
813 if (strcmp("value", src_string
)) {
818 /* Extra identifier, unknown */
832 int is_unary_unsigned(struct bt_list_head
*head
)
835 struct ctf_node
*node
;
837 bt_list_for_each_entry(node
, head
, siblings
) {
838 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
842 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
851 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
855 struct ctf_node
*node
;
857 bt_list_for_each_entry(node
, head
, siblings
) {
858 int uexpr_type
= node
->u
.unary_expression
.type
;
859 int uexpr_link
= node
->u
.unary_expression
.link
;
860 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
861 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
862 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
868 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
877 int is_unary_signed(struct bt_list_head
*head
)
880 struct ctf_node
*node
;
882 bt_list_for_each_entry(node
, head
, siblings
) {
883 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
887 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
896 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
900 struct ctf_node
*node
;
902 bt_list_for_each_entry(node
, head
, siblings
) {
903 int uexpr_type
= node
->u
.unary_expression
.type
;
904 int uexpr_link
= node
->u
.unary_expression
.link
;
905 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
906 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
907 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
908 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
909 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
915 switch (node
->u
.unary_expression
.type
) {
916 case UNARY_UNSIGNED_CONSTANT
:
918 node
->u
.unary_expression
.u
.unsigned_constant
;
920 case UNARY_SIGNED_CONSTANT
:
921 *value
= node
->u
.unary_expression
.u
.signed_constant
;
936 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
940 struct ctf_node
*node
;
942 bt_list_for_each_entry(node
, head
, siblings
) {
943 int uexpr_type
= node
->u
.unary_expression
.type
;
944 int uexpr_link
= node
->u
.unary_expression
.link
;
945 const char *src_string
;
947 if (node
->type
!= NODE_UNARY_EXPRESSION
||
948 uexpr_type
!= UNARY_STRING
||
949 uexpr_link
!= UNARY_LINK_UNKNOWN
||
955 src_string
= node
->u
.unary_expression
.u
.string
;
956 ret
= bt_uuid_parse(src_string
, uuid
);
967 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
971 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
972 _FPERROR(efd
, "%s", "expecting unary expression");
977 switch (unary_expr
->u
.unary_expression
.type
) {
978 case UNARY_UNSIGNED_CONSTANT
:
979 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
981 case UNARY_SIGNED_CONSTANT
:
982 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
986 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
988 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
990 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
993 _FPERROR(efd
, "unexpected string \"%s\"", str
);
1000 _FPERROR(efd
, "%s", "unexpected unary expression type");
1010 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
1011 struct ctf_node
*unary_expr
)
1014 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1016 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1018 "\"byte_order\" attribute: expecting string");
1022 str
= unary_expr
->u
.unary_expression
.u
.string
;
1024 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1025 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1026 } else if (!strcmp(str
, "le")) {
1027 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1028 } else if (!strcmp(str
, "native")) {
1029 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1031 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1041 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1042 struct ctf_node
*uexpr
)
1044 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
1046 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1047 bo
= bt_ctf_trace_get_byte_order(ctx
->trace
);
1054 int is_align_valid(uint64_t align
)
1056 return (align
!= 0) && !(align
& (align
- 1));
1060 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1065 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1070 switch (type_specifier
->u
.type_specifier
.type
) {
1072 g_string_append(str
, "void");
1075 g_string_append(str
, "char");
1077 case TYPESPEC_SHORT
:
1078 g_string_append(str
, "short");
1081 g_string_append(str
, "int");
1084 g_string_append(str
, "long");
1086 case TYPESPEC_FLOAT
:
1087 g_string_append(str
, "float");
1089 case TYPESPEC_DOUBLE
:
1090 g_string_append(str
, "double");
1092 case TYPESPEC_SIGNED
:
1093 g_string_append(str
, "signed");
1095 case TYPESPEC_UNSIGNED
:
1096 g_string_append(str
, "unsigned");
1099 g_string_append(str
, "bool");
1101 case TYPESPEC_COMPLEX
:
1102 g_string_append(str
, "_Complex");
1104 case TYPESPEC_IMAGINARY
:
1105 g_string_append(str
, "_Imaginary");
1107 case TYPESPEC_CONST
:
1108 g_string_append(str
, "const");
1110 case TYPESPEC_ID_TYPE
:
1111 if (type_specifier
->u
.type_specifier
.id_type
) {
1112 g_string_append(str
,
1113 type_specifier
->u
.type_specifier
.id_type
);
1116 case TYPESPEC_STRUCT
:
1118 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1120 if (!node
->u
._struct
.name
) {
1121 _PERROR("%s", "unexpected empty structure name");
1126 g_string_append(str
, "struct ");
1127 g_string_append(str
, node
->u
._struct
.name
);
1130 case TYPESPEC_VARIANT
:
1132 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1134 if (!node
->u
.variant
.name
) {
1135 _PERROR("%s", "unexpected empty variant name");
1140 g_string_append(str
, "variant ");
1141 g_string_append(str
, node
->u
.variant
.name
);
1146 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1148 if (!node
->u
._enum
.enum_id
) {
1149 _PERROR("%s", "unexpected empty enum name");
1154 g_string_append(str
, "enum ");
1155 g_string_append(str
, node
->u
._enum
.enum_id
);
1158 case TYPESPEC_FLOATING_POINT
:
1159 case TYPESPEC_INTEGER
:
1160 case TYPESPEC_STRING
:
1162 _PERROR("%s", "unknown specifier");
1172 int get_type_specifier_list_name(struct ctx
*ctx
,
1173 struct ctf_node
*type_specifier_list
, GString
*str
)
1176 struct ctf_node
*iter
;
1177 int alias_item_nr
= 0;
1178 struct bt_list_head
*head
=
1179 &type_specifier_list
->u
.type_specifier_list
.head
;
1181 bt_list_for_each_entry(iter
, head
, siblings
) {
1182 if (alias_item_nr
!= 0) {
1183 g_string_append(str
, " ");
1187 ret
= get_type_specifier_name(ctx
, iter
, str
);
1198 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1199 struct ctf_node
*type_specifier_list
,
1200 struct ctf_node
*node_type_declarator
)
1206 struct ctf_node
*iter
;
1207 struct bt_list_head
*pointers
=
1208 &node_type_declarator
->u
.type_declarator
.pointers
;
1210 str
= g_string_new("");
1211 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1213 g_string_free(str
, TRUE
);
1217 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1218 g_string_append(str
, " *");
1220 if (iter
->u
.pointer
.const_qualifier
) {
1221 g_string_append(str
, " const");
1225 str_c
= g_string_free(str
, FALSE
);
1226 qalias
= g_quark_from_string(str_c
);
1234 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1235 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1236 struct bt_ctf_field_type
**field_decl
,
1237 struct bt_ctf_field_type
*nested_decl
)
1240 * During this whole function, nested_decl is always OURS,
1241 * whereas field_decl is an output which we create, but
1242 * belongs to the caller (it is moved).
1248 /* Validate type declarator node */
1249 if (node_type_declarator
) {
1250 if (node_type_declarator
->u
.type_declarator
.type
==
1256 /* TODO: GCC bitfields not supported yet */
1257 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1259 _PERROR("%s", "GCC bitfields are not supported as of this version");
1265 /* Find the right nested declaration if not provided */
1267 struct bt_list_head
*pointers
=
1268 &node_type_declarator
->u
.type_declarator
.pointers
;
1270 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1272 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1275 * If we have a pointer declarator, it HAS to
1276 * be present in the typealiases (else fail).
1278 qalias
= create_typealias_identifier(ctx
,
1279 type_specifier_list
, node_type_declarator
);
1281 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1282 g_quark_to_string(qalias
), -1);
1284 _PERROR("cannot find typealias \"%s\"",
1285 g_quark_to_string(qalias
));
1290 /* Make a copy of it */
1291 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1292 BT_PUT(nested_decl
);
1293 if (!nested_decl_copy
) {
1294 _PERROR("%s", "cannot copy nested declaration");
1299 BT_MOVE(nested_decl
, nested_decl_copy
);
1301 /* Force integer's base to 16 since it's a pointer */
1302 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1303 bt_ctf_field_type_integer_set_base(nested_decl
,
1304 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1307 ret
= visit_type_specifier_list(ctx
,
1308 type_specifier_list
, &nested_decl
);
1310 assert(!nested_decl
);
1316 assert(nested_decl
);
1318 if (!node_type_declarator
) {
1319 BT_MOVE(*field_decl
, nested_decl
);
1323 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1324 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1326 node_type_declarator
->u
.type_declarator
.u
.id
;
1328 *field_name
= g_quark_from_string(id
);
1333 BT_MOVE(*field_decl
, nested_decl
);
1336 struct ctf_node
*first
;
1337 _BT_CTF_FIELD_TYPE_INIT(decl
);
1338 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1339 struct bt_list_head
*length
=
1340 &node_type_declarator
->
1341 u
.type_declarator
.u
.nested
.length
;
1343 /* Create array/sequence, pass nested_decl as child */
1344 if (bt_list_empty(length
)) {
1346 "expecting length field reference or value");
1351 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1352 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1357 switch (first
->u
.unary_expression
.type
) {
1358 case UNARY_UNSIGNED_CONSTANT
:
1361 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1363 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1364 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1366 BT_PUT(nested_decl
);
1369 "cannot create array declaration");
1374 BT_MOVE(decl
, array_decl
);
1379 /* Lookup unsigned integer definition, create seq. */
1380 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1381 char *length_name
= concatenate_unary_strings(length
);
1388 seq_decl
= bt_ctf_field_type_sequence_create(
1389 nested_decl
, length_name
);
1390 g_free(length_name
);
1391 BT_PUT(nested_decl
);
1394 "cannot create sequence declaration");
1399 BT_MOVE(decl
, seq_decl
);
1407 assert(!nested_decl
);
1409 assert(!*field_decl
);
1412 * At this point, we found the next nested declaration.
1413 * We currently own this (and lost the ownership of
1414 * nested_decl in the meantime). Pass this next
1415 * nested declaration as the content of the outer
1416 * container, MOVING its ownership.
1418 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1420 node_type_declarator
->
1421 u
.type_declarator
.u
.nested
.type_declarator
,
1422 &outer_field_decl
, decl
);
1425 assert(!outer_field_decl
);
1430 assert(outer_field_decl
);
1431 BT_MOVE(*field_decl
, outer_field_decl
);
1435 BT_PUT(nested_decl
);
1436 assert(*field_decl
);
1441 BT_PUT(nested_decl
);
1442 BT_PUT(*field_decl
);
1448 int visit_struct_decl_field(struct ctx
*ctx
,
1449 struct bt_ctf_field_type
*struct_decl
,
1450 struct ctf_node
*type_specifier_list
,
1451 struct bt_list_head
*type_declarators
)
1454 struct ctf_node
*iter
;
1455 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1457 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1460 const char *field_name
;
1461 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1463 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1464 &qfield_name
, iter
, &field_decl
, NULL
);
1466 assert(!field_decl
);
1467 _PERROR("%s", "unable to find structure field declaration type");
1472 field_name
= g_quark_to_string(qfield_name
);
1474 /* Check if field with same name already exists */
1476 bt_ctf_field_type_structure_get_field_type_by_name(
1477 struct_decl
, field_name
);
1479 BT_PUT(efield_decl
);
1480 _PERROR("duplicate field \"%s\" in structure",
1486 /* Add field to structure */
1487 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1488 field_decl
, field_name
);
1491 _PERROR("cannot add field \"%s\" to structure",
1492 g_quark_to_string(qfield_name
));
1506 int visit_variant_decl_field(struct ctx
*ctx
,
1507 struct bt_ctf_field_type
*variant_decl
,
1508 struct ctf_node
*type_specifier_list
,
1509 struct bt_list_head
*type_declarators
)
1512 struct ctf_node
*iter
;
1513 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1515 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1518 const char *field_name
;
1519 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1521 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1522 &qfield_name
, iter
, &field_decl
, NULL
);
1524 assert(!field_decl
);
1526 "unable to find variant field declaration type");
1531 field_name
= g_quark_to_string(qfield_name
);
1533 /* Check if field with same name already exists */
1535 bt_ctf_field_type_variant_get_field_type_by_name(
1536 variant_decl
, field_name
);
1538 BT_PUT(efield_decl
);
1539 _PERROR("duplicate field \"%s\" in variant",
1545 /* Add field to structure */
1546 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1547 field_decl
, field_name
);
1550 _PERROR("cannot add field \"%s\" to variant",
1551 g_quark_to_string(qfield_name
));
1565 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1566 struct bt_list_head
*type_declarators
)
1570 struct ctf_node
*iter
;
1571 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1573 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1574 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1575 &qidentifier
, iter
, &type_decl
, NULL
);
1577 _PERROR("%s", "problem creating type declaration");
1582 /* Do not allow typedef and typealias of untagged variants */
1583 if (bt_ctf_field_type_is_variant(type_decl
)) {
1584 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1585 _PERROR("%s", "typedef of untagged variant is not allowed");
1591 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1592 g_quark_to_string(qidentifier
), type_decl
);
1594 _PERROR("cannot register typedef \"%s\"",
1595 g_quark_to_string(qidentifier
));
1607 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1608 struct ctf_node
*alias
)
1612 struct ctf_node
*node
;
1613 GQuark qdummy_field_name
;
1614 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1616 /* Create target type declaration */
1617 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1620 node
= _BT_LIST_FIRST_ENTRY(
1621 &target
->u
.typealias_target
.type_declarators
,
1622 struct ctf_node
, siblings
);
1625 ret
= visit_type_declarator(ctx
,
1626 target
->u
.typealias_target
.type_specifier_list
,
1627 &qdummy_field_name
, node
, &type_decl
, NULL
);
1630 _PERROR("%s", "problem creating type declaration");
1634 /* Do not allow typedef and typealias of untagged variants */
1635 if (bt_ctf_field_type_is_variant(type_decl
)) {
1636 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1638 "typealias of untagged variant is not allowed");
1645 * The semantic validator does not check whether the target is
1646 * abstract or not (if it has an identifier). Check it here.
1648 if (qdummy_field_name
!= 0) {
1649 _PERROR("%s", "expecting empty identifier");
1654 /* Create alias identifier */
1655 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1656 struct ctf_node
, siblings
);
1657 qalias
= create_typealias_identifier(ctx
,
1658 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1659 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1660 g_quark_to_string(qalias
), type_decl
);
1662 _PERROR("cannot register typealias \"%s\"",
1663 g_quark_to_string(qalias
));
1674 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1675 struct bt_ctf_field_type
*struct_decl
)
1679 switch (entry_node
->type
) {
1681 ret
= visit_typedef(ctx
,
1682 entry_node
->u
._typedef
.type_specifier_list
,
1683 &entry_node
->u
._typedef
.type_declarators
);
1686 "cannot add typedef in \"struct\" declaration");
1690 case NODE_TYPEALIAS
:
1691 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1692 entry_node
->u
.typealias
.alias
);
1695 "cannot add typealias in \"struct\" declaration");
1699 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1701 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1702 entry_node
->u
.struct_or_variant_declaration
.
1703 type_specifier_list
,
1704 &entry_node
->u
.struct_or_variant_declaration
.
1711 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1721 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1722 struct bt_ctf_field_type
*variant_decl
)
1726 switch (entry_node
->type
) {
1728 ret
= visit_typedef(ctx
,
1729 entry_node
->u
._typedef
.type_specifier_list
,
1730 &entry_node
->u
._typedef
.type_declarators
);
1733 "cannot add typedef in \"variant\" declaration");
1737 case NODE_TYPEALIAS
:
1738 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1739 entry_node
->u
.typealias
.alias
);
1742 "cannot add typealias in \"variant\" declaration");
1746 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1748 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1749 entry_node
->u
.struct_or_variant_declaration
.
1750 type_specifier_list
,
1751 &entry_node
->u
.struct_or_variant_declaration
.
1758 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1768 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1769 struct bt_list_head
*decl_list
, int has_body
,
1770 struct bt_list_head
*min_align
,
1771 struct bt_ctf_field_type
**struct_decl
)
1775 *struct_decl
= NULL
;
1777 /* For named struct (without body), lookup in declaration scope */
1779 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1786 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1788 if (!*struct_decl
) {
1789 _PERROR("cannot find \"struct %s\"", name
);
1794 /* Make a copy of it */
1795 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1796 if (!struct_decl_copy
) {
1798 "cannot create copy of structure declaration");
1803 BT_MOVE(*struct_decl
, struct_decl_copy
);
1805 struct ctf_node
*entry_node
;
1806 uint64_t min_align_value
= 0;
1809 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1811 estruct_decl
= ctx_decl_scope_lookup_struct(
1812 ctx
->current_scope
, name
, 1);
1814 BT_PUT(estruct_decl
);
1815 _PERROR("\"struct %s\" already declared in local scope",
1822 if (!bt_list_empty(min_align
)) {
1823 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1825 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1830 *struct_decl
= bt_ctf_field_type_structure_create();
1831 if (!*struct_decl
) {
1832 _PERROR("%s", "cannot create structure declaration");
1837 if (min_align_value
!= 0) {
1838 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1841 _PERROR("%s", "failed to set structure's minimal alignment");
1846 ret
= ctx_push_scope(ctx
);
1848 _PERROR("%s", "cannot push scope");
1852 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1853 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1864 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1865 name
, *struct_decl
);
1867 _PERROR("cannot register \"struct %s\" in declaration scope",
1877 BT_PUT(*struct_decl
);
1883 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1884 const char *tag
, struct bt_list_head
*decl_list
,
1885 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1888 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1890 *variant_decl
= NULL
;
1892 /* For named variant (without body), lookup in declaration scope */
1894 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1901 untagged_variant_decl
=
1902 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1904 if (!untagged_variant_decl
) {
1905 _PERROR("cannot find \"variant %s\"", name
);
1910 /* Make a copy of it */
1911 variant_decl_copy
= bt_ctf_field_type_copy(
1912 untagged_variant_decl
);
1913 if (!variant_decl_copy
) {
1915 "cannot create copy of structure declaration");
1920 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1922 struct ctf_node
*entry_node
;
1925 struct bt_ctf_field_type
*evariant_decl
=
1926 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1929 if (evariant_decl
) {
1930 BT_PUT(evariant_decl
);
1931 _PERROR("\"variant %s\" already declared in local scope",
1938 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1940 if (!untagged_variant_decl
) {
1941 _PERROR("%s", "cannot create variant declaration");
1946 ret
= ctx_push_scope(ctx
);
1948 _PERROR("%s", "cannot push scope");
1952 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1953 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1954 untagged_variant_decl
);
1964 ret
= ctx_decl_scope_register_variant(
1965 ctx
->current_scope
, name
,
1966 untagged_variant_decl
);
1968 _PERROR("cannot register \"variant %s\" in declaration scope",
1976 * If tagged, create tagged variant and return; otherwise
1977 * return untagged variant.
1980 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1983 * At this point, we have a fresh untagged variant; nobody
1984 * else owns it. Set its tag now.
1986 ret
= bt_ctf_field_type_variant_set_tag_name(
1987 untagged_variant_decl
, tag
);
1992 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1995 assert(!untagged_variant_decl
);
1996 assert(*variant_decl
);
2001 BT_PUT(untagged_variant_decl
);
2002 BT_PUT(*variant_decl
);
2008 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2009 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2013 struct ctf_node
*iter
;
2014 int64_t start
= 0, end
= 0;
2015 const char *label
= enumerator
->u
.enumerator
.id
;
2016 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2018 bt_list_for_each_entry(iter
, values
, siblings
) {
2021 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2022 _PERROR("wrong unary expression for enumeration label \"%s\"",
2034 switch (iter
->u
.unary_expression
.type
) {
2035 case UNARY_SIGNED_CONSTANT
:
2036 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2038 case UNARY_UNSIGNED_CONSTANT
:
2040 iter
->u
.unary_expression
.u
.unsigned_constant
;
2043 _PERROR("invalid enumeration entry: \"%s\"",
2050 _PERROR("invalid enumeration entry: \"%s\"",
2070 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2073 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2074 label
, (uint64_t) start
, (uint64_t) end
);
2077 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2089 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2090 struct ctf_node
*container_type
,
2091 struct bt_list_head
*enumerator_list
,
2093 struct bt_ctf_field_type
**enum_decl
)
2097 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2101 /* For named enum (without body), lookup in declaration scope */
2103 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2110 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2113 _PERROR("cannot find \"enum %s\"", name
);
2118 /* Make a copy of it */
2119 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2120 if (!enum_decl_copy
) {
2122 "cannot create copy of enumeration declaration");
2128 BT_MOVE(*enum_decl
, enum_decl_copy
);
2130 struct ctf_node
*iter
;
2131 int64_t last_value
= 0;
2134 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2136 eenum_decl
= ctx_decl_scope_lookup_enum(
2137 ctx
->current_scope
, name
, 1);
2140 _PERROR("\"enum %s\" already declared in local scope",
2147 if (!container_type
) {
2148 integer_decl
= ctx_decl_scope_lookup_alias(
2149 ctx
->current_scope
, "int", -1);
2150 if (!integer_decl
) {
2151 _PERROR("%s", "cannot find \"int\" type for enumeration");
2156 ret
= visit_type_declarator(ctx
, container_type
,
2157 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2159 assert(!integer_decl
);
2165 assert(integer_decl
);
2167 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2168 _PERROR("%s", "container type for enumeration is not an integer");
2173 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2175 _PERROR("%s", "cannot create enumeration declaration");
2180 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2181 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2183 bt_ctf_field_type_integer_get_signed(integer_decl
));
2190 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2198 BT_PUT(integer_decl
);
2203 BT_PUT(integer_decl
);
2210 int visit_type_specifier(struct ctx
*ctx
,
2211 struct ctf_node
*type_specifier_list
,
2212 struct bt_ctf_field_type
**decl
)
2215 GString
*str
= NULL
;
2216 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2219 str
= g_string_new("");
2220 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2225 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2227 _PERROR("cannot find type alias \"%s\"", str
->str
);
2232 /* Make a copy of the type declaration */
2233 decl_copy
= bt_ctf_field_type_copy(*decl
);
2235 _PERROR("%s", "cannot create copy of type declaration");
2240 BT_MOVE(*decl
, decl_copy
);
2241 (void) g_string_free(str
, TRUE
);
2248 (void) g_string_free(str
, TRUE
);
2257 int visit_integer_decl(struct ctx
*ctx
,
2258 struct bt_list_head
*expressions
,
2259 struct bt_ctf_field_type
**integer_decl
)
2264 struct ctf_node
*expression
;
2265 uint64_t alignment
= 0, size
= 0;
2266 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2267 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2268 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2269 enum bt_ctf_byte_order byte_order
=
2270 bt_ctf_trace_get_byte_order(ctx
->trace
);
2272 *integer_decl
= NULL
;
2274 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2275 struct ctf_node
*left
, *right
;
2277 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2278 struct ctf_node
, siblings
);
2279 right
= _BT_LIST_FIRST_ENTRY(
2280 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2283 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2288 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2289 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2290 _PERROR_DUP_ATTR("signed",
2291 "integer declaration");
2296 signedness
= get_boolean(ctx
->efd
, right
);
2297 if (signedness
< 0) {
2302 _SET(&set
, _INTEGER_SIGNED_SET
);
2303 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2305 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2306 _PERROR_DUP_ATTR("byte_order",
2307 "integer declaration");
2312 byte_order
= get_real_byte_order(ctx
, right
);
2313 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2314 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2319 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2320 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2321 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2322 _PERROR_DUP_ATTR("size",
2323 "integer declaration");
2328 if (right
->u
.unary_expression
.type
!=
2329 UNARY_UNSIGNED_CONSTANT
) {
2330 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2335 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2337 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2340 } else if (size
> 64) {
2341 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2346 _SET(&set
, _INTEGER_SIZE_SET
);
2347 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2349 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2350 _PERROR_DUP_ATTR("align",
2351 "integer declaration");
2356 if (right
->u
.unary_expression
.type
!=
2357 UNARY_UNSIGNED_CONSTANT
) {
2358 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2364 right
->u
.unary_expression
.u
.unsigned_constant
;
2365 if (!is_align_valid(alignment
)) {
2366 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2371 _SET(&set
, _INTEGER_ALIGN_SET
);
2372 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2373 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2374 _PERROR_DUP_ATTR("base", "integer declaration");
2379 switch (right
->u
.unary_expression
.type
) {
2380 case UNARY_UNSIGNED_CONSTANT
:
2382 uint64_t constant
= right
->u
.unary_expression
.
2383 u
.unsigned_constant
;
2387 base
= BT_CTF_INTEGER_BASE_BINARY
;
2390 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2393 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2396 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2399 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2400 right
->u
.unary_expression
.u
.unsigned_constant
);
2408 char *s_right
= concatenate_unary_strings(
2409 &expression
->u
.ctf_expression
.right
);
2411 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2416 if (!strcmp(s_right
, "decimal") ||
2417 !strcmp(s_right
, "dec") ||
2418 !strcmp(s_right
, "d") ||
2419 !strcmp(s_right
, "i") ||
2420 !strcmp(s_right
, "u")) {
2421 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2422 } else if (!strcmp(s_right
, "hexadecimal") ||
2423 !strcmp(s_right
, "hex") ||
2424 !strcmp(s_right
, "x") ||
2425 !strcmp(s_right
, "X") ||
2426 !strcmp(s_right
, "p")) {
2427 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2428 } else if (!strcmp(s_right
, "octal") ||
2429 !strcmp(s_right
, "oct") ||
2430 !strcmp(s_right
, "o")) {
2431 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2432 } else if (!strcmp(s_right
, "binary") ||
2433 !strcmp(s_right
, "b")) {
2434 base
= BT_CTF_INTEGER_BASE_BINARY
;
2436 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2447 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2452 _SET(&set
, _INTEGER_BASE_SET
);
2453 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2457 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2458 _PERROR_DUP_ATTR("encoding",
2459 "integer declaration");
2464 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2465 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2470 s_right
= concatenate_unary_strings(
2471 &expression
->u
.ctf_expression
.right
);
2473 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2478 if (!strcmp(s_right
, "UTF8") ||
2479 !strcmp(s_right
, "utf8") ||
2480 !strcmp(s_right
, "utf-8") ||
2481 !strcmp(s_right
, "UTF-8")) {
2482 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2483 } else if (!strcmp(s_right
, "ASCII") ||
2484 !strcmp(s_right
, "ascii")) {
2485 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2486 } else if (!strcmp(s_right
, "none")) {
2487 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2489 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2497 _SET(&set
, _INTEGER_ENCODING_SET
);
2498 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2499 const char *clock_name
;
2501 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2502 _PERROR_DUP_ATTR("map", "integer declaration");
2507 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2508 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2514 get_map_clock_name_value(
2515 &expression
->u
.ctf_expression
.right
);
2517 char *s_right
= concatenate_unary_strings(
2518 &expression
->u
.ctf_expression
.right
);
2521 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2526 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"",
2528 _SET(&set
, _INTEGER_MAP_SET
);
2533 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2534 ctx
->trace
, clock_name
);
2535 if (!mapped_clock
) {
2536 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"",
2542 _SET(&set
, _INTEGER_MAP_SET
);
2544 _PWARNING("unknown attribute \"%s\" in integer declaration",
2545 left
->u
.unary_expression
.u
.string
);
2549 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2551 "missing \"size\" attribute in integer declaration");
2556 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2557 if (size
% CHAR_BIT
) {
2558 /* Bit-packed alignment */
2561 /* Byte-packed alignment */
2562 alignment
= CHAR_BIT
;
2566 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2567 if (!*integer_decl
) {
2568 _PERROR("%s", "cannot create integer declaration");
2573 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2574 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2575 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2576 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2577 (unsigned int) alignment
);
2578 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2582 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2583 *integer_decl
, mapped_clock
);
2584 bt_put(mapped_clock
);
2585 mapped_clock
= NULL
;
2589 _PERROR("%s", "cannot configure integer declaration");
2598 bt_put(mapped_clock
);
2601 BT_PUT(*integer_decl
);
2607 int visit_floating_point_number_decl(struct ctx
*ctx
,
2608 struct bt_list_head
*expressions
,
2609 struct bt_ctf_field_type
**float_decl
)
2613 struct ctf_node
*expression
;
2614 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2615 enum bt_ctf_byte_order byte_order
=
2616 bt_ctf_trace_get_byte_order(ctx
->trace
);
2620 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2621 struct ctf_node
*left
, *right
;
2623 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2624 struct ctf_node
, siblings
);
2625 right
= _BT_LIST_FIRST_ENTRY(
2626 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2629 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2634 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2635 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2636 _PERROR_DUP_ATTR("byte_order",
2637 "floating point number declaration");
2642 byte_order
= get_real_byte_order(ctx
, right
);
2643 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2644 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2649 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2650 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2652 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2653 _PERROR_DUP_ATTR("exp_dig",
2654 "floating point number declaration");
2659 if (right
->u
.unary_expression
.type
!=
2660 UNARY_UNSIGNED_CONSTANT
) {
2661 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2666 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2667 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2668 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2670 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2671 _PERROR_DUP_ATTR("mant_dig",
2672 "floating point number declaration");
2677 if (right
->u
.unary_expression
.type
!=
2678 UNARY_UNSIGNED_CONSTANT
) {
2679 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2684 mant_dig
= right
->u
.unary_expression
.u
.
2686 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2687 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2689 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2690 _PERROR_DUP_ATTR("align",
2691 "floating point number declaration");
2696 if (right
->u
.unary_expression
.type
!=
2697 UNARY_UNSIGNED_CONSTANT
) {
2698 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2703 alignment
= right
->u
.unary_expression
.u
.
2706 if (!is_align_valid(alignment
)) {
2707 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2712 _SET(&set
, _FLOAT_ALIGN_SET
);
2714 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2715 left
->u
.unary_expression
.u
.string
);
2719 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2720 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2725 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2726 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2731 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2732 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2733 /* Bit-packed alignment */
2736 /* Byte-packed alignment */
2737 alignment
= CHAR_BIT
;
2741 *float_decl
= bt_ctf_field_type_floating_point_create();
2744 "cannot create floating point number declaration");
2749 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2750 *float_decl
, exp_dig
);
2751 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2752 *float_decl
, mant_dig
);
2753 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2754 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2757 "cannot configure floating point number declaration");
2765 BT_PUT(*float_decl
);
2771 int visit_string_decl(struct ctx
*ctx
,
2772 struct bt_list_head
*expressions
,
2773 struct bt_ctf_field_type
**string_decl
)
2777 struct ctf_node
*expression
;
2778 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2780 *string_decl
= NULL
;
2782 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2783 struct ctf_node
*left
, *right
;
2785 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2786 struct ctf_node
, siblings
);
2787 right
= _BT_LIST_FIRST_ENTRY(
2788 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2791 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2796 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2799 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2800 _PERROR_DUP_ATTR("encoding",
2801 "string declaration");
2806 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2807 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2812 s_right
= concatenate_unary_strings(
2813 &expression
->u
.ctf_expression
.right
);
2815 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2820 if (!strcmp(s_right
, "UTF8") ||
2821 !strcmp(s_right
, "utf8") ||
2822 !strcmp(s_right
, "utf-8") ||
2823 !strcmp(s_right
, "UTF-8")) {
2824 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2825 } else if (!strcmp(s_right
, "ASCII") ||
2826 !strcmp(s_right
, "ascii")) {
2827 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2828 } else if (!strcmp(s_right
, "none")) {
2829 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2831 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2839 _SET(&set
, _STRING_ENCODING_SET
);
2841 _PWARNING("unknown attribute \"%s\" in string declaration",
2842 left
->u
.unary_expression
.u
.string
);
2846 *string_decl
= bt_ctf_field_type_string_create();
2847 if (!*string_decl
) {
2848 _PERROR("%s", "cannot create string declaration");
2853 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2855 _PERROR("%s", "cannot configure string declaration");
2863 BT_PUT(*string_decl
);
2869 int visit_type_specifier_list(struct ctx
*ctx
,
2870 struct ctf_node
*ts_list
,
2871 struct bt_ctf_field_type
**decl
)
2874 struct ctf_node
*first
, *node
;
2878 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2883 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2884 struct ctf_node
, siblings
);
2885 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2890 node
= first
->u
.type_specifier
.node
;
2892 switch (first
->u
.type_specifier
.type
) {
2893 case TYPESPEC_INTEGER
:
2894 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2901 case TYPESPEC_FLOATING_POINT
:
2902 ret
= visit_floating_point_number_decl(ctx
,
2903 &node
->u
.floating_point
.expressions
, decl
);
2909 case TYPESPEC_STRING
:
2910 ret
= visit_string_decl(ctx
,
2911 &node
->u
.string
.expressions
, decl
);
2917 case TYPESPEC_STRUCT
:
2918 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2919 &node
->u
._struct
.declaration_list
,
2920 node
->u
._struct
.has_body
,
2921 &node
->u
._struct
.min_align
, decl
);
2927 case TYPESPEC_VARIANT
:
2928 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2929 node
->u
.variant
.choice
,
2930 &node
->u
.variant
.declaration_list
,
2931 node
->u
.variant
.has_body
, decl
);
2938 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2939 node
->u
._enum
.container_type
,
2940 &node
->u
._enum
.enumerator_list
,
2941 node
->u
._enum
.has_body
, decl
);
2949 case TYPESPEC_SHORT
:
2952 case TYPESPEC_FLOAT
:
2953 case TYPESPEC_DOUBLE
:
2954 case TYPESPEC_SIGNED
:
2955 case TYPESPEC_UNSIGNED
:
2957 case TYPESPEC_COMPLEX
:
2958 case TYPESPEC_IMAGINARY
:
2959 case TYPESPEC_CONST
:
2960 case TYPESPEC_ID_TYPE
:
2961 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2968 _PERROR("unexpected node type: %d",
2969 (int) first
->u
.type_specifier
.type
);
2985 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2986 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2991 _BT_CTF_FIELD_TYPE_INIT(decl
);
2993 switch (node
->type
) {
2995 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2996 &node
->u
._typedef
.type_declarators
);
2999 "cannot add typedef in \"event\" declaration");
3003 case NODE_TYPEALIAS
:
3004 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3005 node
->u
.typealias
.alias
);
3007 _PERROR("%s", "cannot add typealias in \"event\" declaration");
3011 case NODE_CTF_EXPRESSION
:
3013 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3019 if (!strcmp(left
, "name")) {
3020 /* This is already known at this stage */
3021 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3022 _PERROR_DUP_ATTR("name", "event declaration");
3027 _SET(set
, _EVENT_NAME_SET
);
3028 } else if (!strcmp(left
, "id")) {
3031 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3032 _PERROR_DUP_ATTR("id", "event declaration");
3037 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3039 if (ret
|| id
< 0) {
3040 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3045 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3048 "cannot set event declaration's ID");
3052 _SET(set
, _EVENT_ID_SET
);
3053 } else if (!strcmp(left
, "stream_id")) {
3054 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3055 _PERROR_DUP_ATTR("stream_id",
3056 "event declaration");
3061 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3062 (uint64_t *) stream_id
);
3063 if (ret
|| *stream_id
< 0) {
3064 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3069 _SET(set
, _EVENT_STREAM_ID_SET
);
3070 } else if (!strcmp(left
, "context")) {
3071 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3072 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3077 ret
= visit_type_specifier_list(ctx
,
3078 _BT_LIST_FIRST_ENTRY(
3079 &node
->u
.ctf_expression
.right
,
3080 struct ctf_node
, siblings
),
3083 _PERROR("%s", "cannot create event context declaration");
3088 ret
= bt_ctf_event_class_set_context_type(
3092 _PERROR("%s", "cannot set event's context declaration");
3096 _SET(set
, _EVENT_CONTEXT_SET
);
3097 } else if (!strcmp(left
, "fields")) {
3098 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3099 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3104 ret
= visit_type_specifier_list(ctx
,
3105 _BT_LIST_FIRST_ENTRY(
3106 &node
->u
.ctf_expression
.right
,
3107 struct ctf_node
, siblings
),
3110 _PERROR("%s", "cannot create event payload declaration");
3115 ret
= bt_ctf_event_class_set_payload_type(
3119 _PERROR("%s", "cannot set event's payload declaration");
3123 _SET(set
, _EVENT_FIELDS_SET
);
3124 } else if (!strcmp(left
, "loglevel")) {
3125 uint64_t loglevel_value
;
3126 const char *loglevel_str
;
3127 struct bt_value
*value_obj
, *str_obj
;
3129 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3130 _PERROR_DUP_ATTR("loglevel",
3131 "event declaration");
3136 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3139 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3143 value_obj
= bt_value_integer_create_init(loglevel_value
);
3145 _PERROR("%s", "cannot allocate memory for loglevel value object");
3149 if (bt_ctf_event_class_set_attribute(event_class
,
3150 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3151 _PERROR("%s", "cannot set loglevel value");
3156 loglevel_str
= print_loglevel(loglevel_value
);
3158 str_obj
= bt_value_string_create_init(loglevel_str
);
3159 if (bt_ctf_event_class_set_attribute(event_class
,
3160 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3161 _PERROR("%s", "cannot set loglevel string");
3169 _SET(set
, _EVENT_LOGLEVEL_SET
);
3170 } else if (!strcmp(left
, "model.emf.uri")) {
3173 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3174 _PERROR_DUP_ATTR("model.emf.uri",
3175 "event declaration");
3180 right
= concatenate_unary_strings(
3181 &node
->u
.ctf_expression
.right
);
3183 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3188 // TODO: FIXME: set model EMF URI here
3191 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3193 _PWARNING("unknown attribute \"%s\" in event declaration",
3219 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3223 struct ctf_node
*iter
;
3224 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3226 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3227 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3231 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3236 if (!strcmp(left
, "name")) {
3237 name
= concatenate_unary_strings(
3238 &iter
->u
.ctf_expression
.right
);
3240 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3262 int reset_event_decl_types(struct ctx
*ctx
,
3263 struct bt_ctf_event_class
*event_class
)
3268 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3270 _PERROR("%s", "cannot set initial NULL event context");
3274 /* Event payload. */
3275 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3277 _PERROR("%s", "cannot set initial NULL event payload");
3285 int reset_stream_decl_types(struct ctx
*ctx
,
3286 struct bt_ctf_stream_class
*stream_class
)
3290 /* Packet context. */
3291 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3293 _PERROR("%s", "cannot set initial empty packet context");
3298 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3300 _PERROR("%s", "cannot set initial empty event header");
3304 /* Event context. */
3305 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3307 _PERROR("%s", "cannot set initial empty stream event context");
3315 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3318 struct bt_ctf_stream_class
*stream_class
;
3320 stream_class
= bt_ctf_stream_class_create(NULL
);
3321 if (!stream_class
) {
3322 _PERROR("%s", "cannot create stream class");
3327 * Set packet context, event header, and event context to NULL to
3328 * override the default ones.
3330 ret
= reset_stream_decl_types(ctx
, stream_class
);
3335 return stream_class
;
3338 BT_PUT(stream_class
);
3344 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3349 struct ctf_node
*iter
;
3350 int64_t stream_id
= -1;
3351 char *event_name
= NULL
;
3352 struct bt_ctf_event_class
*event_class
= NULL
;
3353 struct bt_ctf_event_class
*eevent_class
;
3354 struct bt_ctf_stream_class
*stream_class
= NULL
;
3355 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3356 bool pop_scope
= false;
3358 if (node
->visited
) {
3362 node
->visited
= TRUE
;
3363 event_name
= get_event_decl_name(ctx
, node
);
3366 "missing \"name\" attribute in event declaration");
3371 event_class
= bt_ctf_event_class_create(event_name
);
3374 * Unset context and fields to override the default ones.
3376 ret
= reset_event_decl_types(ctx
, event_class
);
3381 ret
= ctx_push_scope(ctx
);
3383 _PERROR("%s", "cannot push scope");
3389 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3390 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3397 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3399 struct bt_ctf_stream_class
*new_stream_class
;
3400 size_t stream_class_count
=
3401 g_hash_table_size(ctx
->stream_classes
) +
3402 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3405 * Allow missing stream_id if there is only a single
3408 switch (stream_class_count
) {
3410 /* Create implicit stream class if there's none */
3411 new_stream_class
= create_reset_stream_class(ctx
);
3412 if (!new_stream_class
) {
3417 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3419 _PERROR("%s", "cannot set stream class's ID");
3420 BT_PUT(new_stream_class
);
3426 /* Move reference to visitor's context */
3427 g_hash_table_insert(ctx
->stream_classes
,
3428 (gpointer
) stream_id
, new_stream_class
);
3429 new_stream_class
= NULL
;
3432 /* Single stream class: get its ID */
3433 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3434 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3435 stream_id
= (int64_t) keys
->data
;
3438 assert(bt_ctf_trace_get_stream_class_count(
3441 bt_ctf_trace_get_stream_class_by_index(
3443 assert(stream_class
);
3444 stream_id
= bt_ctf_stream_class_get_id(
3446 BT_PUT(stream_class
);
3450 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3456 assert(stream_id
>= 0);
3458 /* We have the stream ID now; get the stream class if found */
3459 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3460 (gpointer
) stream_id
);
3461 bt_get(stream_class
);
3462 if (!stream_class
) {
3463 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3465 if (!stream_class
) {
3466 _PERROR("cannot find stream class with ID %" PRId64
,
3473 assert(stream_class
);
3475 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3476 /* Allow only one event without ID per stream */
3477 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3480 "missing \"id\" field in event declaration");
3486 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3488 _PERROR("%s", "cannot set event's ID");
3493 event_id
= bt_ctf_event_class_get_id(event_class
);
3495 _PERROR("%s", "cannot get event's ID");
3500 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3503 BT_PUT(eevent_class
);
3504 _PERROR("%s", "duplicate event with ID %" PRId64
" in same stream");
3509 eevent_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
,
3512 BT_PUT(eevent_class
);
3514 "duplicate event with name \"%s\" in same stream");
3519 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3520 BT_PUT(event_class
);
3522 _PERROR("%s", "cannot add event class to stream class");
3529 bt_put(event_class
);
3537 bt_put(stream_class
);
3542 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3543 struct bt_ctf_stream_class
*stream_class
, int *set
)
3547 _BT_CTF_FIELD_TYPE_INIT(decl
);
3549 switch (node
->type
) {
3551 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3552 &node
->u
._typedef
.type_declarators
);
3555 "cannot add typedef in \"stream\" declaration");
3559 case NODE_TYPEALIAS
:
3560 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3561 node
->u
.typealias
.alias
);
3563 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3567 case NODE_CTF_EXPRESSION
:
3569 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3575 if (!strcmp(left
, "id")) {
3579 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3580 _PERROR_DUP_ATTR("id", "stream declaration");
3585 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3587 if (ret
|| id
< 0) {
3588 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3593 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3596 _PERROR("duplicate stream with ID %" PRId64
,
3602 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3605 "cannot set stream declaration's ID");
3609 _SET(set
, _STREAM_ID_SET
);
3610 } else if (!strcmp(left
, "event.header")) {
3611 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3612 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3617 ret
= visit_type_specifier_list(ctx
,
3618 _BT_LIST_FIRST_ENTRY(
3619 &node
->u
.ctf_expression
.right
,
3620 struct ctf_node
, siblings
),
3623 _PERROR("%s", "cannot create event header declaration");
3629 ret
= bt_ctf_stream_class_set_event_header_type(
3630 stream_class
, decl
);
3633 _PERROR("%s", "cannot set stream's event header declaration");
3637 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3638 } else if (!strcmp(left
, "event.context")) {
3639 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3640 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3645 ret
= visit_type_specifier_list(ctx
,
3646 _BT_LIST_FIRST_ENTRY(
3647 &node
->u
.ctf_expression
.right
,
3648 struct ctf_node
, siblings
),
3651 _PERROR("%s", "cannot create stream event context declaration");
3657 ret
= bt_ctf_stream_class_set_event_context_type(
3658 stream_class
, decl
);
3661 _PERROR("%s", "cannot set stream's event context declaration");
3665 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3666 } else if (!strcmp(left
, "packet.context")) {
3667 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3668 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3673 ret
= visit_type_specifier_list(ctx
,
3674 _BT_LIST_FIRST_ENTRY(
3675 &node
->u
.ctf_expression
.right
,
3676 struct ctf_node
, siblings
),
3679 _PERROR("%s", "cannot create packet context declaration");
3685 ret
= bt_ctf_stream_class_set_packet_context_type(
3686 stream_class
, decl
);
3689 _PERROR("%s", "cannot set stream's packet context declaration");
3693 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3695 _PWARNING("unknown attribute \"%s\" in stream declaration",
3719 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3724 struct ctf_node
*iter
;
3725 struct bt_ctf_stream_class
*stream_class
= NULL
;
3726 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3727 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3729 if (node
->visited
) {
3733 node
->visited
= TRUE
;
3734 stream_class
= create_reset_stream_class(ctx
);
3735 if (!stream_class
) {
3740 ret
= ctx_push_scope(ctx
);
3742 _PERROR("%s", "cannot push scope");
3746 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3747 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3756 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3757 /* Check that packet header has stream_id field */
3758 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3759 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3761 packet_header_decl
=
3762 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3763 if (!packet_header_decl
) {
3765 "cannot get trace packet header declaration");
3770 bt_ctf_field_type_structure_get_field_type_by_name(
3771 packet_header_decl
, "stream_id");
3772 BT_PUT(packet_header_decl
);
3773 if (!stream_id_decl
) {
3774 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3778 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3779 BT_PUT(stream_id_decl
);
3780 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3784 BT_PUT(stream_id_decl
);
3786 /* Allow only _one_ ID-less stream */
3787 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3789 "missing \"id\" field in stream declaration");
3794 /* Automatic ID: 0 */
3795 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3798 id
= bt_ctf_stream_class_get_id(stream_class
);
3800 _PERROR("wrong stream ID: %" PRId64
, id
);
3806 * Make sure that this stream class's ID is currently unique in
3809 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
)) {
3810 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3815 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3817 if (existing_stream_class
) {
3818 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3823 /* Move reference to visitor's context */
3824 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3826 stream_class
= NULL
;
3830 bt_put(stream_class
);
3833 bt_put(existing_stream_class
);
3838 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3842 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3844 switch (node
->type
) {
3846 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3847 &node
->u
._typedef
.type_declarators
);
3850 "cannot add typedef in \"trace\" declaration");
3854 case NODE_TYPEALIAS
:
3855 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3856 node
->u
.typealias
.alias
);
3859 "cannot add typealias in \"trace\" declaration");
3863 case NODE_CTF_EXPRESSION
:
3865 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3871 if (!strcmp(left
, "major")) {
3872 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3873 _PERROR_DUP_ATTR("major", "trace declaration");
3878 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3881 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3886 _SET(set
, _TRACE_MAJOR_SET
);
3887 } else if (!strcmp(left
, "minor")) {
3888 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3889 _PERROR_DUP_ATTR("minor", "trace declaration");
3894 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3897 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3902 _SET(set
, _TRACE_MINOR_SET
);
3903 } else if (!strcmp(left
, "uuid")) {
3904 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3905 _PERROR_DUP_ATTR("uuid", "trace declaration");
3910 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3914 "invalid trace declaration's UUID");
3918 _SET(set
, _TRACE_UUID_SET
);
3919 } else if (!strcmp(left
, "byte_order")) {
3920 /* Native byte order is already known at this stage */
3921 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3922 _PERROR_DUP_ATTR("byte_order",
3923 "trace declaration");
3928 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3929 } else if (!strcmp(left
, "packet.header")) {
3930 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3931 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3936 ret
= visit_type_specifier_list(ctx
,
3937 _BT_LIST_FIRST_ENTRY(
3938 &node
->u
.ctf_expression
.right
,
3939 struct ctf_node
, siblings
),
3940 &packet_header_decl
);
3942 _PERROR("%s", "cannot create packet header declaration");
3946 assert(packet_header_decl
);
3947 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3948 packet_header_decl
);
3949 BT_PUT(packet_header_decl
);
3951 _PERROR("%s", "cannot set trace declaration's packet header declaration");
3955 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3957 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3965 _PERROR("%s", "unknown expression in trace declaration");
3974 BT_PUT(packet_header_decl
);
3980 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3984 struct ctf_node
*iter
;
3985 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3987 if (node
->visited
) {
3991 node
->visited
= TRUE
;
3993 if (ctx
->is_trace_visited
) {
3994 _PERROR("%s", "duplicate \"trace\" block");
3999 ret
= ctx_push_scope(ctx
);
4001 _PERROR("%s", "cannot push scope");
4005 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4006 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4015 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4017 "missing \"major\" attribute in trace declaration");
4022 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4024 "missing \"minor\" attribute in trace declaration");
4029 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4030 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4035 ctx
->is_trace_visited
= TRUE
;
4045 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4049 struct ctf_node
*entry_node
;
4050 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4052 if (node
->visited
) {
4056 node
->visited
= TRUE
;
4058 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4059 struct bt_list_head
*right_head
=
4060 &entry_node
->u
.ctf_expression
.right
;
4062 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4063 _PERROR("%s", "wrong expression in environment entry");
4068 left
= concatenate_unary_strings(
4069 &entry_node
->u
.ctf_expression
.left
);
4071 _PERROR("%s", "cannot get environment entry name");
4076 if (is_unary_string(right_head
)) {
4077 char *right
= concatenate_unary_strings(right_head
);
4080 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4086 if (strcmp(left
, "tracer_name") == 0) {
4087 if (strncmp(right
, "lttng", 5) == 0) {
4092 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4093 ret
= bt_ctf_trace_set_environment_field_string(
4094 ctx
->trace
, left
, right
);
4098 _PERROR("environment: cannot add entry \"%s\" to trace",
4102 } else if (is_unary_unsigned(right_head
) ||
4103 is_unary_signed(right_head
)) {
4106 if (is_unary_unsigned(right_head
)) {
4107 ret
= get_unary_unsigned(right_head
,
4110 ret
= get_unary_signed(right_head
, &v
);
4113 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4119 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4120 ret
= bt_ctf_trace_set_environment_field_integer(
4121 ctx
->trace
, left
, v
);
4123 _PERROR("environment: cannot add entry \"%s\" to trace",
4128 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4146 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4151 struct ctf_node
*node
;
4152 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4154 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4155 if (node
->type
== NODE_CTF_EXPRESSION
) {
4156 struct ctf_node
*right_node
;
4158 left
= concatenate_unary_strings(
4159 &node
->u
.ctf_expression
.left
);
4165 if (!strcmp(left
, "byte_order")) {
4166 enum bt_ctf_byte_order bo
;
4168 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4169 _PERROR_DUP_ATTR("byte_order",
4170 "trace declaration");
4175 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4176 right_node
= _BT_LIST_FIRST_ENTRY(
4177 &node
->u
.ctf_expression
.right
,
4178 struct ctf_node
, siblings
);
4179 bo
= byte_order_from_unary_expr(ctx
->efd
,
4181 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4182 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4185 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4186 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4192 ret
= bt_ctf_trace_set_native_byte_order(
4195 _PERROR("cannot set trace's byte order (%d)",
4206 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4207 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4221 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4222 struct bt_ctf_clock_class
*clock
, int *set
)
4227 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4232 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4238 if (!strcmp(left
, "name")) {
4241 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4242 _PERROR_DUP_ATTR("name", "clock class declaration");
4247 right
= concatenate_unary_strings(
4248 &entry_node
->u
.ctf_expression
.right
);
4250 _PERROR("%s", "unexpected unary expression for clock class declaration's \"name\" attribute");
4255 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4257 _PERROR("%s", "cannot set clock class's name");
4263 _SET(set
, _CLOCK_NAME_SET
);
4264 } else if (!strcmp(left
, "uuid")) {
4265 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4267 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4268 _PERROR_DUP_ATTR("uuid", "clock class declaration");
4273 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4275 _PERROR("%s", "invalid clock class UUID");
4279 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4281 _PERROR("%s", "cannot set clock class's UUID");
4285 _SET(set
, _CLOCK_UUID_SET
);
4286 } else if (!strcmp(left
, "description")) {
4289 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4290 _PERROR_DUP_ATTR("description", "clock class declaration");
4295 right
= concatenate_unary_strings(
4296 &entry_node
->u
.ctf_expression
.right
);
4298 _PERROR("%s", "unexpected unary expression for clock class's \"description\" attribute");
4303 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4305 _PERROR("%s", "cannot set clock class's description");
4311 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4312 } else if (!strcmp(left
, "freq")) {
4315 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4316 _PERROR_DUP_ATTR("freq", "clock class declaration");
4321 ret
= get_unary_unsigned(
4322 &entry_node
->u
.ctf_expression
.right
, &freq
);
4324 _PERROR("%s", "unexpected unary expression for clock class declaration's \"freq\" attribute");
4329 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4331 _PERROR("%s", "cannot set clock class's frequency");
4335 _SET(set
, _CLOCK_FREQ_SET
);
4336 } else if (!strcmp(left
, "precision")) {
4339 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4340 _PERROR_DUP_ATTR("precision", "clock class declaration");
4345 ret
= get_unary_unsigned(
4346 &entry_node
->u
.ctf_expression
.right
, &precision
);
4348 _PERROR("%s", "unexpected unary expression for clock class declaration's \"precision\" attribute");
4353 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4355 _PERROR("%s", "cannot set clock class's precision");
4359 _SET(set
, _CLOCK_PRECISION_SET
);
4360 } else if (!strcmp(left
, "offset_s")) {
4363 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4364 _PERROR_DUP_ATTR("offset_s", "clock class declaration");
4369 ret
= get_unary_unsigned(
4370 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4372 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset_s\" attribute");
4377 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4379 _PERROR("%s", "cannot set clock class's offset in seconds");
4383 _SET(set
, _CLOCK_OFFSET_S_SET
);
4384 } else if (!strcmp(left
, "offset")) {
4387 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4388 _PERROR_DUP_ATTR("offset", "clock class declaration");
4393 ret
= get_unary_unsigned(
4394 &entry_node
->u
.ctf_expression
.right
, &offset
);
4396 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset\" attribute");
4401 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4403 _PERROR("%s", "cannot set clock class's offset in cycles");
4407 _SET(set
, _CLOCK_OFFSET_SET
);
4408 } else if (!strcmp(left
, "absolute")) {
4409 struct ctf_node
*right
;
4411 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4412 _PERROR_DUP_ATTR("absolute", "clock class declaration");
4417 right
= _BT_LIST_FIRST_ENTRY(
4418 &entry_node
->u
.ctf_expression
.right
,
4419 struct ctf_node
, siblings
);
4420 ret
= get_boolean(ctx
->efd
, right
);
4422 _PERROR("%s", "unexpected unary expression for clock class declaration's \"absolute\" attribute");
4427 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4429 _PERROR("%s", "cannot set clock class's absolute option");
4433 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4435 _PWARNING("unknown attribute \"%s\" in clock class declaration",
4451 uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4456 if (frequency
== 1000000000ULL) {
4459 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4466 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4470 int64_t offset_cycles
;
4472 freq
= bt_ctf_clock_class_get_frequency(clock
);
4473 if (freq
== -1ULL) {
4474 _PERROR("%s", "cannot get clock class frequency");
4479 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4481 _PERROR("%s", "cannot get offset in cycles");
4486 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4487 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4494 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4498 struct bt_ctf_clock_class
*clock
;
4499 struct ctf_node
*entry_node
;
4500 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4501 const char *clock_class_name
;
4503 if (clock_node
->visited
) {
4507 clock_node
->visited
= TRUE
;
4508 clock
= bt_ctf_clock_class_create(NULL
);
4510 _PERROR("%s", "cannot create clock");
4515 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4516 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4522 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4524 "missing \"name\" attribute in clock class declaration");
4529 clock_class_name
= bt_ctf_clock_class_get_name(clock
);
4530 assert(clock_class_name
);
4531 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4533 * Old versions of LTTng forgot to set its clock class
4534 * as absolute, even if it is. This is important because
4535 * it's a condition to be able to sort notifications
4536 * from different sources.
4538 ret
= bt_ctf_clock_class_set_is_absolute(clock
, 1);
4540 _PERROR("%s", "cannot set clock class's absolute option");
4545 ret
= apply_clock_class_offset(ctx
, clock
);
4547 _PERROR("%s", "cannot apply clock class offset ");
4551 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4553 _PERROR("%s", "cannot add clock class to trace");
4564 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4568 if (root_decl_node
->visited
) {
4572 root_decl_node
->visited
= TRUE
;
4574 switch (root_decl_node
->type
) {
4576 ret
= visit_typedef(ctx
,
4577 root_decl_node
->u
._typedef
.type_specifier_list
,
4578 &root_decl_node
->u
._typedef
.type_declarators
);
4580 _PERROR("%s", "cannot add typedef in root scope");
4584 case NODE_TYPEALIAS
:
4585 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4586 root_decl_node
->u
.typealias
.alias
);
4588 _PERROR("%s", "cannot add typealias in root scope");
4592 case NODE_TYPE_SPECIFIER_LIST
:
4594 _BT_CTF_FIELD_TYPE_INIT(decl
);
4597 * Just add the type specifier to the root
4598 * declaration scope. Put local reference.
4600 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4619 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4622 GHashTableIter iter
;
4623 gpointer key
, stream_class
;
4625 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4627 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4628 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4631 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4632 _PERROR("cannot add stream class %" PRId64
" to trace",
4638 g_hash_table_remove_all(ctx
->stream_classes
);
4645 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(FILE *efd
,
4646 uint64_t clock_class_offset_ns
)
4649 struct ctx
*ctx
= NULL
;
4650 struct bt_ctf_trace
*trace
;
4652 trace
= bt_ctf_trace_create();
4654 _FPERROR(efd
, "%s", "cannot create trace");
4658 /* Set packet header to NULL to override the default one */
4659 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
4662 "cannot set initial, empty packet header structure");
4666 /* Create visitor's context */
4667 ctx
= ctx_create(trace
, efd
, clock_class_offset_ns
);
4669 _FPERROR(efd
, "%s", "cannot create visitor context");
4682 return (void *) ctx
;
4686 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4688 ctx_destroy((void *) visitor
);
4692 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
4693 struct ctf_visitor_generate_ir
*visitor
)
4695 struct ctx
*ctx
= (void *) visitor
;
4699 return bt_get(ctx
->trace
);
4703 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4704 struct ctf_node
*node
)
4707 struct ctx
*ctx
= (void *) visitor
;
4709 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4711 switch (node
->type
) {
4714 struct ctf_node
*iter
;
4715 int got_trace_decl
= FALSE
;
4716 int found_callsite
= FALSE
;
4719 * The first thing we need is the native byte order of
4720 * the trace block, because early type aliases can have
4721 * a `byte_order` attribute set to `native`. If we don't
4722 * have the native byte order yet, and we don't have any
4723 * trace block yet, then fail with EINCOMPLETE.
4725 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4726 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4727 if (got_trace_decl
) {
4728 _PERROR("%s", "duplicate trace declaration");
4733 ret
= set_trace_byte_order(ctx
, iter
);
4735 _PERROR("cannot set trace's native byte order (%d)",
4740 got_trace_decl
= TRUE
;
4743 if (!got_trace_decl
) {
4749 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
4750 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
4751 assert(ctx
->current_scope
&&
4752 ctx
->current_scope
->parent_scope
== NULL
);
4755 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4756 ret
= visit_env(ctx
, iter
);
4758 _PERROR("error while visiting environment block (%d)",
4764 assert(ctx
->current_scope
&&
4765 ctx
->current_scope
->parent_scope
== NULL
);
4768 * Visit clock blocks.
4770 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4771 ret
= visit_clock_decl(ctx
, iter
);
4773 _PERROR("error while visiting clock class declaration (%d)",
4779 assert(ctx
->current_scope
&&
4780 ctx
->current_scope
->parent_scope
== NULL
);
4783 * Visit root declarations next, as they can be used by any
4786 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4788 ret
= visit_root_decl(ctx
, iter
);
4790 _PERROR("error while visiting root declaration (%d)",
4796 assert(ctx
->current_scope
&&
4797 ctx
->current_scope
->parent_scope
== NULL
);
4799 /* Callsite blocks are not supported */
4800 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4801 found_callsite
= TRUE
;
4805 assert(ctx
->current_scope
&&
4806 ctx
->current_scope
->parent_scope
== NULL
);
4808 if (found_callsite
) {
4809 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4813 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4814 ret
= visit_trace_decl(ctx
, iter
);
4816 _PERROR("%s", "error while visiting trace declaration");
4821 assert(ctx
->current_scope
&&
4822 ctx
->current_scope
->parent_scope
== NULL
);
4825 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4826 ret
= visit_stream_decl(ctx
, iter
);
4828 _PERROR("%s", "error while visiting stream declaration");
4833 assert(ctx
->current_scope
&&
4834 ctx
->current_scope
->parent_scope
== NULL
);
4837 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4838 ret
= visit_event_decl(ctx
, iter
);
4840 _PERROR("%s", "error while visiting event declaration");
4845 assert(ctx
->current_scope
&&
4846 ctx
->current_scope
->parent_scope
== NULL
);
4850 _PERROR("cannot decode node type: %d", (int) node
->type
);
4855 /* Move decoded stream classes to trace, if any */
4856 ret
= move_ctx_stream_classes_to_trace(ctx
);
4858 _PERROR("%s", "cannot move stream classes to trace");