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.h>
40 #include <babeltrace/endian.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 /* Offset (ns) to apply to clock classes on creation */
223 uint64_t clock_class_offset_ns
;
225 /* Trace attributes */
226 enum bt_ctf_byte_order trace_bo
;
227 uint64_t trace_major
;
228 uint64_t trace_minor
;
229 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
232 * Stream IDs to stream classes.
234 * int64_t -> struct bt_ctf_stream_class *
236 GHashTable
*stream_classes
;
242 struct ctf_visitor_generate_ir
{ };
245 const char *loglevel_str
[] = {
246 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
247 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
248 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
249 [ LOGLEVEL_ERR
] = "TRACE_ERR",
250 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
251 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
252 [ LOGLEVEL_INFO
] = "TRACE_INFO",
253 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
254 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
255 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
256 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
257 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
258 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
259 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
260 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
264 const char *print_loglevel(int64_t value
)
269 if (value
>= _NR_LOGLEVELS
) {
270 return "<<UNKNOWN>>";
272 return loglevel_str
[value
];
276 * Creates a new declaration scope.
278 * @param par_scope Parent scope (NULL if creating a root scope)
279 * @returns New declaration scope, or NULL on error
282 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
284 struct ctx_decl_scope
*scope
;
286 scope
= g_new(struct ctx_decl_scope
, 1);
291 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
292 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
293 scope
->parent_scope
= par_scope
;
300 * Destroys a declaration scope.
302 * This function does not destroy the parent scope.
304 * @param scope Scope to destroy
307 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
313 g_hash_table_destroy(scope
->decl_map
);
321 * Returns the GQuark of a prefixed alias.
323 * @param prefix Prefix character
325 * @returns Associated GQuark, or 0 on error
328 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
334 /* Prefix character + original string + '\0' */
335 char *prname
= g_new(char, strlen(name
) + 2);
340 sprintf(prname
, "%c%s", prefix
, name
);
341 qname
= g_quark_from_string(prname
);
349 * Looks up a prefixed type alias within a declaration scope.
351 * @param scope Declaration scope
352 * @param prefix Prefix character
353 * @param name Alias name
354 * @param level Number of levels to dig (-1 means infinite)
355 * @returns Declaration, or NULL if not found
358 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
359 struct ctx_decl_scope
*scope
, char prefix
,
360 const char *name
, int levels
)
364 _BT_CTF_FIELD_TYPE_INIT(decl
);
365 struct ctx_decl_scope
*cur_scope
= scope
;
369 qname
= get_prefixed_named_quark(prefix
, name
);
378 while (cur_scope
&& cur_levels
< levels
) {
379 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
380 (gconstpointer
) (unsigned long) qname
);
382 /* Caller's reference */
387 cur_scope
= cur_scope
->parent_scope
;
398 * Looks up a type alias within a declaration scope.
400 * @param scope Declaration scope
401 * @param name Alias name
402 * @param level Number of levels to dig (-1 means infinite)
403 * @returns Declaration, or NULL if not found
406 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
407 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
409 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
414 * Looks up an enumeration within a declaration scope.
416 * @param scope Declaration scope
417 * @param name Enumeration name
418 * @param level Number of levels to dig (-1 means infinite)
419 * @returns Declaration, or NULL if not found
422 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
423 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
425 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
430 * Looks up a structure within a declaration scope.
432 * @param scope Declaration scope
433 * @param name Structure name
434 * @param level Number of levels to dig (-1 means infinite)
435 * @returns Declaration, or NULL if not found
438 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
439 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
441 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
446 * Looks up a variant within a declaration scope.
448 * @param scope Declaration scope
449 * @param name Variant name
450 * @param level Number of levels to dig (-1 means infinite)
451 * @returns Declaration, or NULL if not found
454 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
455 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
457 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
462 * Registers a prefixed type alias within a declaration scope.
464 * @param scope Declaration scope
465 * @param prefix Prefix character
466 * @param name Alias name (non-NULL)
467 * @param decl Declaration to register
468 * @returns 0 if registration went okay, negative value otherwise
471 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
472 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
476 _BT_CTF_FIELD_TYPE_INIT(edecl
);
481 qname
= get_prefixed_named_quark(prefix
, name
);
487 /* Make sure alias does not exist in local scope */
488 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
495 g_hash_table_insert(scope
->decl_map
,
496 (gpointer
) (unsigned long) qname
, decl
);
498 /* Hash table's reference */
508 * Registers a type alias within a declaration scope.
510 * @param scope Declaration scope
511 * @param name Alias name (non-NULL)
512 * @param decl Declaration to register
513 * @returns 0 if registration went okay, negative value otherwise
516 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
517 const char *name
, struct bt_ctf_field_type
*decl
)
519 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
524 * Registers an enumeration declaration within a declaration scope.
526 * @param scope Declaration scope
527 * @param name Enumeration name (non-NULL)
528 * @param decl Enumeration declaration to register
529 * @returns 0 if registration went okay, negative value otherwise
532 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
533 const char *name
, struct bt_ctf_field_type
*decl
)
535 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
540 * Registers a structure declaration within a declaration scope.
542 * @param scope Declaration scope
543 * @param name Structure name (non-NULL)
544 * @param decl Structure declaration to register
545 * @returns 0 if registration went okay, negative value otherwise
548 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
549 const char *name
, struct bt_ctf_field_type
*decl
)
551 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
556 * Registers a variant declaration within a declaration scope.
558 * @param scope Declaration scope
559 * @param name Variant name (non-NULL)
560 * @param decl Variant declaration to register
561 * @returns 0 if registration went okay, negative value otherwise
564 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
565 const char *name
, struct bt_ctf_field_type
*decl
)
567 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
572 * Creates a new visitor context.
574 * @param trace Associated trace
575 * @param efd Error stream
576 * @returns New visitor context, or NULL on error
579 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
,
580 uint64_t clock_class_offset_ns
)
582 struct ctx
*ctx
= NULL
;
583 struct ctx_decl_scope
*scope
= NULL
;
585 ctx
= g_new0(struct ctx
, 1);
590 /* Root declaration scope */
591 scope
= ctx_decl_scope_create(NULL
);
596 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
597 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
598 if (!ctx
->stream_classes
) {
604 ctx
->current_scope
= scope
;
605 ctx
->trace_bo
= BT_CTF_BYTE_ORDER_NATIVE
;
606 ctx
->clock_class_offset_ns
= clock_class_offset_ns
;
611 ctx_decl_scope_destroy(scope
);
616 * Destroys a visitor context.
618 * @param ctx Visitor context to destroy
621 void ctx_destroy(struct ctx
*ctx
)
623 struct ctx_decl_scope
*scope
;
625 * Destroy all scopes, from current one to the root scope.
632 scope
= ctx
->current_scope
;
635 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
637 ctx_decl_scope_destroy(scope
);
638 scope
= parent_scope
;
642 g_hash_table_destroy(ctx
->stream_classes
);
650 * Pushes a new declaration scope on top of a visitor context's
651 * declaration scope stack.
653 * @param ctx Visitor context
654 * @returns 0 on success, or a negative value on error
657 int ctx_push_scope(struct ctx
*ctx
)
660 struct ctx_decl_scope
*new_scope
;
663 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
669 ctx
->current_scope
= new_scope
;
676 void ctx_pop_scope(struct ctx
*ctx
)
678 struct ctx_decl_scope
*parent_scope
= NULL
;
682 if (!ctx
->current_scope
) {
686 parent_scope
= ctx
->current_scope
->parent_scope
;
687 ctx_decl_scope_destroy(ctx
->current_scope
);
688 ctx
->current_scope
= parent_scope
;
695 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
696 struct bt_ctf_field_type
**decl
);
699 int is_unary_string(struct bt_list_head
*head
)
702 struct ctf_node
*node
;
704 bt_list_for_each_entry(node
, head
, siblings
) {
705 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
709 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
718 char *concatenate_unary_strings(struct bt_list_head
*head
)
722 struct ctf_node
*node
;
724 str
= g_string_new(NULL
);
726 bt_list_for_each_entry(node
, head
, siblings
) {
730 node
->type
!= NODE_UNARY_EXPRESSION
||
731 node
->u
.unary_expression
.type
!= UNARY_STRING
||
734 node
->u
.unary_expression
.link
!=
742 switch (node
->u
.unary_expression
.link
) {
744 g_string_append(str
, ".");
746 case UNARY_ARROWLINK
:
747 g_string_append(str
, "->");
749 case UNARY_DOTDOTDOT
:
750 g_string_append(str
, "...");
756 src_string
= node
->u
.unary_expression
.u
.string
;
757 g_string_append(str
, src_string
);
761 /* Destroys the container, returns the underlying string */
762 return g_string_free(str
, FALSE
);
765 /* This always returns NULL */
766 return g_string_free(str
, TRUE
);
770 const char *get_map_clock_name_value(struct bt_list_head
*head
)
773 struct ctf_node
*node
;
774 const char *name
= NULL
;
776 bt_list_for_each_entry(node
, head
, siblings
) {
778 int uexpr_type
= node
->u
.unary_expression
.type
;
779 int uexpr_link
= node
->u
.unary_expression
.link
;
780 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
781 uexpr_type
!= UNARY_STRING
||
782 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
787 /* Needs to be chained with . */
788 switch (node
->u
.unary_expression
.link
) {
791 case UNARY_ARROWLINK
:
792 case UNARY_DOTDOTDOT
:
798 src_string
= node
->u
.unary_expression
.u
.string
;
802 if (strcmp("clock", src_string
)) {
810 if (strcmp("value", src_string
)) {
815 /* Extra identifier, unknown */
829 int is_unary_unsigned(struct bt_list_head
*head
)
832 struct ctf_node
*node
;
834 bt_list_for_each_entry(node
, head
, siblings
) {
835 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
839 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
848 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
852 struct ctf_node
*node
;
854 bt_list_for_each_entry(node
, head
, siblings
) {
855 int uexpr_type
= node
->u
.unary_expression
.type
;
856 int uexpr_link
= node
->u
.unary_expression
.link
;
857 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
858 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
859 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
865 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
874 int is_unary_signed(struct bt_list_head
*head
)
877 struct ctf_node
*node
;
879 bt_list_for_each_entry(node
, head
, siblings
) {
880 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
884 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
893 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
897 struct ctf_node
*node
;
899 bt_list_for_each_entry(node
, head
, siblings
) {
900 int uexpr_type
= node
->u
.unary_expression
.type
;
901 int uexpr_link
= node
->u
.unary_expression
.link
;
902 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
903 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
904 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
905 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
906 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
912 switch (node
->u
.unary_expression
.type
) {
913 case UNARY_UNSIGNED_CONSTANT
:
915 node
->u
.unary_expression
.u
.unsigned_constant
;
917 case UNARY_SIGNED_CONSTANT
:
918 *value
= node
->u
.unary_expression
.u
.signed_constant
;
933 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
937 struct ctf_node
*node
;
939 bt_list_for_each_entry(node
, head
, siblings
) {
940 int uexpr_type
= node
->u
.unary_expression
.type
;
941 int uexpr_link
= node
->u
.unary_expression
.link
;
942 const char *src_string
;
944 if (node
->type
!= NODE_UNARY_EXPRESSION
||
945 uexpr_type
!= UNARY_STRING
||
946 uexpr_link
!= UNARY_LINK_UNKNOWN
||
952 src_string
= node
->u
.unary_expression
.u
.string
;
953 ret
= bt_uuid_parse(src_string
, uuid
);
964 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
968 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
969 _FPERROR(efd
, "%s", "expecting unary expression");
974 switch (unary_expr
->u
.unary_expression
.type
) {
975 case UNARY_UNSIGNED_CONSTANT
:
976 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
978 case UNARY_SIGNED_CONSTANT
:
979 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
983 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
985 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
987 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
990 _FPERROR(efd
, "unexpected string \"%s\"", str
);
997 _FPERROR(efd
, "%s", "unexpected unary expression type");
1007 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
1008 struct ctf_node
*unary_expr
)
1011 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1013 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1015 "\"byte_order\" attribute: expecting string");
1019 str
= unary_expr
->u
.unary_expression
.u
.string
;
1021 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1022 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1023 } else if (!strcmp(str
, "le")) {
1024 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1025 } else if (!strcmp(str
, "native")) {
1026 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1028 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1038 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1039 struct ctf_node
*uexpr
)
1041 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
1043 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1044 bo
= bt_ctf_trace_get_byte_order(ctx
->trace
);
1051 int is_align_valid(uint64_t align
)
1053 return (align
!= 0) && !(align
& (align
- 1));
1057 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1062 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1067 switch (type_specifier
->u
.type_specifier
.type
) {
1069 g_string_append(str
, "void");
1072 g_string_append(str
, "char");
1074 case TYPESPEC_SHORT
:
1075 g_string_append(str
, "short");
1078 g_string_append(str
, "int");
1081 g_string_append(str
, "long");
1083 case TYPESPEC_FLOAT
:
1084 g_string_append(str
, "float");
1086 case TYPESPEC_DOUBLE
:
1087 g_string_append(str
, "double");
1089 case TYPESPEC_SIGNED
:
1090 g_string_append(str
, "signed");
1092 case TYPESPEC_UNSIGNED
:
1093 g_string_append(str
, "unsigned");
1096 g_string_append(str
, "bool");
1098 case TYPESPEC_COMPLEX
:
1099 g_string_append(str
, "_Complex");
1101 case TYPESPEC_IMAGINARY
:
1102 g_string_append(str
, "_Imaginary");
1104 case TYPESPEC_CONST
:
1105 g_string_append(str
, "const");
1107 case TYPESPEC_ID_TYPE
:
1108 if (type_specifier
->u
.type_specifier
.id_type
) {
1109 g_string_append(str
,
1110 type_specifier
->u
.type_specifier
.id_type
);
1113 case TYPESPEC_STRUCT
:
1115 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1117 if (!node
->u
._struct
.name
) {
1118 _PERROR("%s", "unexpected empty structure name");
1123 g_string_append(str
, "struct ");
1124 g_string_append(str
, node
->u
._struct
.name
);
1127 case TYPESPEC_VARIANT
:
1129 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1131 if (!node
->u
.variant
.name
) {
1132 _PERROR("%s", "unexpected empty variant name");
1137 g_string_append(str
, "variant ");
1138 g_string_append(str
, node
->u
.variant
.name
);
1143 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1145 if (!node
->u
._enum
.enum_id
) {
1146 _PERROR("%s", "unexpected empty enum name");
1151 g_string_append(str
, "enum ");
1152 g_string_append(str
, node
->u
._enum
.enum_id
);
1155 case TYPESPEC_FLOATING_POINT
:
1156 case TYPESPEC_INTEGER
:
1157 case TYPESPEC_STRING
:
1159 _PERROR("%s", "unknown specifier");
1169 int get_type_specifier_list_name(struct ctx
*ctx
,
1170 struct ctf_node
*type_specifier_list
, GString
*str
)
1173 struct ctf_node
*iter
;
1174 int alias_item_nr
= 0;
1175 struct bt_list_head
*head
=
1176 &type_specifier_list
->u
.type_specifier_list
.head
;
1178 bt_list_for_each_entry(iter
, head
, siblings
) {
1179 if (alias_item_nr
!= 0) {
1180 g_string_append(str
, " ");
1184 ret
= get_type_specifier_name(ctx
, iter
, str
);
1195 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1196 struct ctf_node
*type_specifier_list
,
1197 struct ctf_node
*node_type_declarator
)
1203 struct ctf_node
*iter
;
1204 struct bt_list_head
*pointers
=
1205 &node_type_declarator
->u
.type_declarator
.pointers
;
1207 str
= g_string_new("");
1208 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1210 g_string_free(str
, TRUE
);
1214 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1215 g_string_append(str
, " *");
1217 if (iter
->u
.pointer
.const_qualifier
) {
1218 g_string_append(str
, " const");
1222 str_c
= g_string_free(str
, FALSE
);
1223 qalias
= g_quark_from_string(str_c
);
1231 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1232 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1233 struct bt_ctf_field_type
**field_decl
,
1234 struct bt_ctf_field_type
*nested_decl
)
1237 * During this whole function, nested_decl is always OURS,
1238 * whereas field_decl is an output which we create, but
1239 * belongs to the caller (it is moved).
1245 /* Validate type declarator node */
1246 if (node_type_declarator
) {
1247 if (node_type_declarator
->u
.type_declarator
.type
==
1253 /* TODO: GCC bitfields not supported yet */
1254 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1256 _PERROR("%s", "GCC bitfields are not supported as of this version");
1262 /* Find the right nested declaration if not provided */
1264 struct bt_list_head
*pointers
=
1265 &node_type_declarator
->u
.type_declarator
.pointers
;
1267 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1269 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1272 * If we have a pointer declarator, it HAS to
1273 * be present in the typealiases (else fail).
1275 qalias
= create_typealias_identifier(ctx
,
1276 type_specifier_list
, node_type_declarator
);
1278 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1279 g_quark_to_string(qalias
), -1);
1281 _PERROR("cannot find typealias \"%s\"",
1282 g_quark_to_string(qalias
));
1287 /* Make a copy of it */
1288 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1289 BT_PUT(nested_decl
);
1290 if (!nested_decl_copy
) {
1291 _PERROR("%s", "cannot copy nested declaration");
1296 BT_MOVE(nested_decl
, nested_decl_copy
);
1298 /* Force integer's base to 16 since it's a pointer */
1299 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1300 bt_ctf_field_type_integer_set_base(nested_decl
,
1301 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1304 ret
= visit_type_specifier_list(ctx
,
1305 type_specifier_list
, &nested_decl
);
1307 assert(!nested_decl
);
1313 assert(nested_decl
);
1315 if (!node_type_declarator
) {
1316 BT_MOVE(*field_decl
, nested_decl
);
1320 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1321 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1323 node_type_declarator
->u
.type_declarator
.u
.id
;
1325 *field_name
= g_quark_from_string(id
);
1330 BT_MOVE(*field_decl
, nested_decl
);
1333 struct ctf_node
*first
;
1334 _BT_CTF_FIELD_TYPE_INIT(decl
);
1335 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1336 struct bt_list_head
*length
=
1337 &node_type_declarator
->
1338 u
.type_declarator
.u
.nested
.length
;
1340 /* Create array/sequence, pass nested_decl as child */
1341 if (bt_list_empty(length
)) {
1343 "expecting length field reference or value");
1348 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1349 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1354 switch (first
->u
.unary_expression
.type
) {
1355 case UNARY_UNSIGNED_CONSTANT
:
1358 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1360 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1361 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1363 BT_PUT(nested_decl
);
1366 "cannot create array declaration");
1371 BT_MOVE(decl
, array_decl
);
1376 /* Lookup unsigned integer definition, create seq. */
1377 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1378 char *length_name
= concatenate_unary_strings(length
);
1385 seq_decl
= bt_ctf_field_type_sequence_create(
1386 nested_decl
, length_name
);
1387 g_free(length_name
);
1388 BT_PUT(nested_decl
);
1391 "cannot create sequence declaration");
1396 BT_MOVE(decl
, seq_decl
);
1404 assert(!nested_decl
);
1406 assert(!*field_decl
);
1409 * At this point, we found the next nested declaration.
1410 * We currently own this (and lost the ownership of
1411 * nested_decl in the meantime). Pass this next
1412 * nested declaration as the content of the outer
1413 * container, MOVING its ownership.
1415 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1417 node_type_declarator
->
1418 u
.type_declarator
.u
.nested
.type_declarator
,
1419 &outer_field_decl
, decl
);
1422 assert(!outer_field_decl
);
1427 assert(outer_field_decl
);
1428 BT_MOVE(*field_decl
, outer_field_decl
);
1432 BT_PUT(nested_decl
);
1433 assert(*field_decl
);
1438 BT_PUT(nested_decl
);
1439 BT_PUT(*field_decl
);
1445 int visit_struct_decl_field(struct ctx
*ctx
,
1446 struct bt_ctf_field_type
*struct_decl
,
1447 struct ctf_node
*type_specifier_list
,
1448 struct bt_list_head
*type_declarators
)
1451 struct ctf_node
*iter
;
1452 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1454 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1457 const char *field_name
;
1458 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1460 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1461 &qfield_name
, iter
, &field_decl
, NULL
);
1463 assert(!field_decl
);
1464 _PERROR("%s", "unable to find structure field declaration type");
1469 field_name
= g_quark_to_string(qfield_name
);
1471 /* Check if field with same name already exists */
1473 bt_ctf_field_type_structure_get_field_type_by_name(
1474 struct_decl
, field_name
);
1476 BT_PUT(efield_decl
);
1477 _PERROR("duplicate field \"%s\" in structure",
1483 /* Add field to structure */
1484 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1485 field_decl
, field_name
);
1488 _PERROR("cannot add field \"%s\" to structure",
1489 g_quark_to_string(qfield_name
));
1503 int visit_variant_decl_field(struct ctx
*ctx
,
1504 struct bt_ctf_field_type
*variant_decl
,
1505 struct ctf_node
*type_specifier_list
,
1506 struct bt_list_head
*type_declarators
)
1509 struct ctf_node
*iter
;
1510 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1512 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1515 const char *field_name
;
1516 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1518 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1519 &qfield_name
, iter
, &field_decl
, NULL
);
1521 assert(!field_decl
);
1523 "unable to find variant field declaration type");
1528 field_name
= g_quark_to_string(qfield_name
);
1530 /* Check if field with same name already exists */
1532 bt_ctf_field_type_variant_get_field_type_by_name(
1533 variant_decl
, field_name
);
1535 BT_PUT(efield_decl
);
1536 _PERROR("duplicate field \"%s\" in variant",
1542 /* Add field to structure */
1543 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1544 field_decl
, field_name
);
1547 _PERROR("cannot add field \"%s\" to variant",
1548 g_quark_to_string(qfield_name
));
1562 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1563 struct bt_list_head
*type_declarators
)
1567 struct ctf_node
*iter
;
1568 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1570 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1571 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1572 &qidentifier
, iter
, &type_decl
, NULL
);
1574 _PERROR("%s", "problem creating type declaration");
1579 /* Do not allow typedef and typealias of untagged variants */
1580 if (bt_ctf_field_type_is_variant(type_decl
)) {
1581 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1582 _PERROR("%s", "typedef of untagged variant is not allowed");
1588 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1589 g_quark_to_string(qidentifier
), type_decl
);
1591 _PERROR("cannot register typedef \"%s\"",
1592 g_quark_to_string(qidentifier
));
1604 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1605 struct ctf_node
*alias
)
1609 struct ctf_node
*node
;
1610 GQuark qdummy_field_name
;
1611 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1613 /* Create target type declaration */
1614 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1617 node
= _BT_LIST_FIRST_ENTRY(
1618 &target
->u
.typealias_target
.type_declarators
,
1619 struct ctf_node
, siblings
);
1622 ret
= visit_type_declarator(ctx
,
1623 target
->u
.typealias_target
.type_specifier_list
,
1624 &qdummy_field_name
, node
, &type_decl
, NULL
);
1627 _PERROR("%s", "problem creating type declaration");
1631 /* Do not allow typedef and typealias of untagged variants */
1632 if (bt_ctf_field_type_is_variant(type_decl
)) {
1633 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1635 "typealias of untagged variant is not allowed");
1642 * The semantic validator does not check whether the target is
1643 * abstract or not (if it has an identifier). Check it here.
1645 if (qdummy_field_name
!= 0) {
1646 _PERROR("%s", "expecting empty identifier");
1651 /* Create alias identifier */
1652 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1653 struct ctf_node
, siblings
);
1654 qalias
= create_typealias_identifier(ctx
,
1655 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1656 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1657 g_quark_to_string(qalias
), type_decl
);
1659 _PERROR("cannot register typealias \"%s\"",
1660 g_quark_to_string(qalias
));
1671 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1672 struct bt_ctf_field_type
*struct_decl
)
1676 switch (entry_node
->type
) {
1678 ret
= visit_typedef(ctx
,
1679 entry_node
->u
._typedef
.type_specifier_list
,
1680 &entry_node
->u
._typedef
.type_declarators
);
1683 "cannot add typedef in \"struct\" declaration");
1687 case NODE_TYPEALIAS
:
1688 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1689 entry_node
->u
.typealias
.alias
);
1692 "cannot add typealias in \"struct\" declaration");
1696 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1698 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1699 entry_node
->u
.struct_or_variant_declaration
.
1700 type_specifier_list
,
1701 &entry_node
->u
.struct_or_variant_declaration
.
1708 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1718 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1719 struct bt_ctf_field_type
*variant_decl
)
1723 switch (entry_node
->type
) {
1725 ret
= visit_typedef(ctx
,
1726 entry_node
->u
._typedef
.type_specifier_list
,
1727 &entry_node
->u
._typedef
.type_declarators
);
1730 "cannot add typedef in \"variant\" declaration");
1734 case NODE_TYPEALIAS
:
1735 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1736 entry_node
->u
.typealias
.alias
);
1739 "cannot add typealias in \"variant\" declaration");
1743 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1745 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1746 entry_node
->u
.struct_or_variant_declaration
.
1747 type_specifier_list
,
1748 &entry_node
->u
.struct_or_variant_declaration
.
1755 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1765 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1766 struct bt_list_head
*decl_list
, int has_body
,
1767 struct bt_list_head
*min_align
,
1768 struct bt_ctf_field_type
**struct_decl
)
1772 *struct_decl
= NULL
;
1774 /* For named struct (without body), lookup in declaration scope */
1776 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1783 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1785 if (!*struct_decl
) {
1786 _PERROR("cannot find \"struct %s\"", name
);
1791 /* Make a copy of it */
1792 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1793 if (!struct_decl_copy
) {
1795 "cannot create copy of structure declaration");
1800 BT_MOVE(*struct_decl
, struct_decl_copy
);
1802 struct ctf_node
*entry_node
;
1803 uint64_t min_align_value
= 0;
1806 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1808 estruct_decl
= ctx_decl_scope_lookup_struct(
1809 ctx
->current_scope
, name
, 1);
1811 BT_PUT(estruct_decl
);
1812 _PERROR("\"struct %s\" already declared in local scope",
1819 if (!bt_list_empty(min_align
)) {
1820 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1822 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1827 *struct_decl
= bt_ctf_field_type_structure_create();
1828 if (!*struct_decl
) {
1829 _PERROR("%s", "cannot create structure declaration");
1834 if (min_align_value
!= 0) {
1835 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1838 _PERROR("%s", "failed to set structure's minimal alignment");
1843 ret
= ctx_push_scope(ctx
);
1845 _PERROR("%s", "cannot push scope");
1849 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1850 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1861 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1862 name
, *struct_decl
);
1864 _PERROR("cannot register \"struct %s\" in declaration scope",
1874 BT_PUT(*struct_decl
);
1880 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1881 const char *tag
, struct bt_list_head
*decl_list
,
1882 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1885 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1887 *variant_decl
= NULL
;
1889 /* For named variant (without body), lookup in declaration scope */
1891 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1898 untagged_variant_decl
=
1899 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1901 if (!untagged_variant_decl
) {
1902 _PERROR("cannot find \"variant %s\"", name
);
1907 /* Make a copy of it */
1908 variant_decl_copy
= bt_ctf_field_type_copy(
1909 untagged_variant_decl
);
1910 if (!variant_decl_copy
) {
1912 "cannot create copy of structure declaration");
1917 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1919 struct ctf_node
*entry_node
;
1922 struct bt_ctf_field_type
*evariant_decl
=
1923 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1926 if (evariant_decl
) {
1927 BT_PUT(evariant_decl
);
1928 _PERROR("\"variant %s\" already declared in local scope",
1935 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1937 if (!untagged_variant_decl
) {
1938 _PERROR("%s", "cannot create variant declaration");
1943 ret
= ctx_push_scope(ctx
);
1945 _PERROR("%s", "cannot push scope");
1949 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1950 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1951 untagged_variant_decl
);
1961 ret
= ctx_decl_scope_register_variant(
1962 ctx
->current_scope
, name
,
1963 untagged_variant_decl
);
1965 _PERROR("cannot register \"variant %s\" in declaration scope",
1973 * If tagged, create tagged variant and return; otherwise
1974 * return untagged variant.
1977 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1980 * At this point, we have a fresh untagged variant; nobody
1981 * else owns it. Set its tag now.
1983 ret
= bt_ctf_field_type_variant_set_tag_name(
1984 untagged_variant_decl
, tag
);
1989 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1992 assert(!untagged_variant_decl
);
1993 assert(*variant_decl
);
1998 BT_PUT(untagged_variant_decl
);
1999 BT_PUT(*variant_decl
);
2005 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
2006 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
2010 struct ctf_node
*iter
;
2011 int64_t start
= 0, end
= 0;
2012 const char *label
= enumerator
->u
.enumerator
.id
;
2013 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2015 bt_list_for_each_entry(iter
, values
, siblings
) {
2018 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2019 _PERROR("wrong unary expression for enumeration label \"%s\"",
2031 switch (iter
->u
.unary_expression
.type
) {
2032 case UNARY_SIGNED_CONSTANT
:
2033 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2035 case UNARY_UNSIGNED_CONSTANT
:
2037 iter
->u
.unary_expression
.u
.unsigned_constant
;
2040 _PERROR("invalid enumeration entry: \"%s\"",
2047 _PERROR("invalid enumeration entry: \"%s\"",
2067 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2070 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2071 label
, (uint64_t) start
, (uint64_t) end
);
2074 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2086 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2087 struct ctf_node
*container_type
,
2088 struct bt_list_head
*enumerator_list
,
2090 struct bt_ctf_field_type
**enum_decl
)
2094 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2098 /* For named enum (without body), lookup in declaration scope */
2100 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2107 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2110 _PERROR("cannot find \"enum %s\"", name
);
2115 /* Make a copy of it */
2116 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2117 if (!enum_decl_copy
) {
2119 "cannot create copy of enumeration declaration");
2125 BT_MOVE(*enum_decl
, enum_decl_copy
);
2127 struct ctf_node
*iter
;
2128 int64_t last_value
= 0;
2131 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2133 eenum_decl
= ctx_decl_scope_lookup_enum(
2134 ctx
->current_scope
, name
, 1);
2137 _PERROR("\"enum %s\" already declared in local scope",
2144 if (!container_type
) {
2145 integer_decl
= ctx_decl_scope_lookup_alias(
2146 ctx
->current_scope
, "int", -1);
2147 if (!integer_decl
) {
2148 _PERROR("%s", "cannot find \"int\" type for enumeration");
2153 ret
= visit_type_declarator(ctx
, container_type
,
2154 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2156 assert(!integer_decl
);
2162 assert(integer_decl
);
2164 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2165 _PERROR("%s", "container type for enumeration is not an integer");
2170 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2172 _PERROR("%s", "cannot create enumeration declaration");
2177 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2178 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2180 bt_ctf_field_type_integer_get_signed(integer_decl
));
2187 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2195 BT_PUT(integer_decl
);
2200 BT_PUT(integer_decl
);
2207 int visit_type_specifier(struct ctx
*ctx
,
2208 struct ctf_node
*type_specifier_list
,
2209 struct bt_ctf_field_type
**decl
)
2212 GString
*str
= NULL
;
2213 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2216 str
= g_string_new("");
2217 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2222 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2224 _PERROR("cannot find type alias \"%s\"", str
->str
);
2229 /* Make a copy of the type declaration */
2230 decl_copy
= bt_ctf_field_type_copy(*decl
);
2232 _PERROR("%s", "cannot create copy of type declaration");
2237 BT_MOVE(*decl
, decl_copy
);
2238 (void) g_string_free(str
, TRUE
);
2245 (void) g_string_free(str
, TRUE
);
2254 int visit_integer_decl(struct ctx
*ctx
,
2255 struct bt_list_head
*expressions
,
2256 struct bt_ctf_field_type
**integer_decl
)
2261 struct ctf_node
*expression
;
2262 uint64_t alignment
= 0, size
= 0;
2263 struct bt_ctf_clock_class
*mapped_clock
= NULL
;
2264 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2265 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2266 enum bt_ctf_byte_order byte_order
=
2267 bt_ctf_trace_get_byte_order(ctx
->trace
);
2269 *integer_decl
= NULL
;
2271 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2272 struct ctf_node
*left
, *right
;
2274 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2275 struct ctf_node
, siblings
);
2276 right
= _BT_LIST_FIRST_ENTRY(
2277 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2280 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2285 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2286 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2287 _PERROR_DUP_ATTR("signed",
2288 "integer declaration");
2293 signedness
= get_boolean(ctx
->efd
, right
);
2294 if (signedness
< 0) {
2299 _SET(&set
, _INTEGER_SIGNED_SET
);
2300 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2302 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2303 _PERROR_DUP_ATTR("byte_order",
2304 "integer declaration");
2309 byte_order
= get_real_byte_order(ctx
, right
);
2310 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2311 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2316 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2317 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2318 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2319 _PERROR_DUP_ATTR("size",
2320 "integer declaration");
2325 if (right
->u
.unary_expression
.type
!=
2326 UNARY_UNSIGNED_CONSTANT
) {
2327 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2332 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2334 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2337 } else if (size
> 64) {
2338 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2343 _SET(&set
, _INTEGER_SIZE_SET
);
2344 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2346 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2347 _PERROR_DUP_ATTR("align",
2348 "integer declaration");
2353 if (right
->u
.unary_expression
.type
!=
2354 UNARY_UNSIGNED_CONSTANT
) {
2355 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2361 right
->u
.unary_expression
.u
.unsigned_constant
;
2362 if (!is_align_valid(alignment
)) {
2363 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2368 _SET(&set
, _INTEGER_ALIGN_SET
);
2369 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2370 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2371 _PERROR_DUP_ATTR("base", "integer declaration");
2376 switch (right
->u
.unary_expression
.type
) {
2377 case UNARY_UNSIGNED_CONSTANT
:
2379 uint64_t constant
= right
->u
.unary_expression
.
2380 u
.unsigned_constant
;
2384 base
= BT_CTF_INTEGER_BASE_BINARY
;
2387 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2390 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2393 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2396 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2397 right
->u
.unary_expression
.u
.unsigned_constant
);
2405 char *s_right
= concatenate_unary_strings(
2406 &expression
->u
.ctf_expression
.right
);
2408 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2413 if (!strcmp(s_right
, "decimal") ||
2414 !strcmp(s_right
, "dec") ||
2415 !strcmp(s_right
, "d") ||
2416 !strcmp(s_right
, "i") ||
2417 !strcmp(s_right
, "u")) {
2418 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2419 } else if (!strcmp(s_right
, "hexadecimal") ||
2420 !strcmp(s_right
, "hex") ||
2421 !strcmp(s_right
, "x") ||
2422 !strcmp(s_right
, "X") ||
2423 !strcmp(s_right
, "p")) {
2424 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2425 } else if (!strcmp(s_right
, "octal") ||
2426 !strcmp(s_right
, "oct") ||
2427 !strcmp(s_right
, "o")) {
2428 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2429 } else if (!strcmp(s_right
, "binary") ||
2430 !strcmp(s_right
, "b")) {
2431 base
= BT_CTF_INTEGER_BASE_BINARY
;
2433 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2444 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2449 _SET(&set
, _INTEGER_BASE_SET
);
2450 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2454 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2455 _PERROR_DUP_ATTR("encoding",
2456 "integer declaration");
2461 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2462 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2467 s_right
= concatenate_unary_strings(
2468 &expression
->u
.ctf_expression
.right
);
2470 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2475 if (!strcmp(s_right
, "UTF8") ||
2476 !strcmp(s_right
, "utf8") ||
2477 !strcmp(s_right
, "utf-8") ||
2478 !strcmp(s_right
, "UTF-8")) {
2479 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2480 } else if (!strcmp(s_right
, "ASCII") ||
2481 !strcmp(s_right
, "ascii")) {
2482 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2483 } else if (!strcmp(s_right
, "none")) {
2484 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2486 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2494 _SET(&set
, _INTEGER_ENCODING_SET
);
2495 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2496 const char *clock_name
;
2498 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2499 _PERROR_DUP_ATTR("map", "integer declaration");
2504 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2505 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2511 get_map_clock_name_value(
2512 &expression
->u
.ctf_expression
.right
);
2514 char *s_right
= concatenate_unary_strings(
2515 &expression
->u
.ctf_expression
.right
);
2518 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2523 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock class: \"%s\"",
2525 _SET(&set
, _INTEGER_MAP_SET
);
2530 mapped_clock
= bt_ctf_trace_get_clock_class_by_name(
2531 ctx
->trace
, clock_name
);
2532 if (!mapped_clock
) {
2533 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock class \"%s\"",
2539 _SET(&set
, _INTEGER_MAP_SET
);
2541 _PWARNING("unknown attribute \"%s\" in integer declaration",
2542 left
->u
.unary_expression
.u
.string
);
2546 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2548 "missing \"size\" attribute in integer declaration");
2553 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2554 if (size
% CHAR_BIT
) {
2555 /* Bit-packed alignment */
2558 /* Byte-packed alignment */
2559 alignment
= CHAR_BIT
;
2563 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2564 if (!*integer_decl
) {
2565 _PERROR("%s", "cannot create integer declaration");
2570 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2571 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2572 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2573 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2574 (unsigned int) alignment
);
2575 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2579 ret
|= bt_ctf_field_type_integer_set_mapped_clock_class(
2580 *integer_decl
, mapped_clock
);
2581 bt_put(mapped_clock
);
2582 mapped_clock
= NULL
;
2586 _PERROR("%s", "cannot configure integer declaration");
2595 bt_put(mapped_clock
);
2598 BT_PUT(*integer_decl
);
2604 int visit_floating_point_number_decl(struct ctx
*ctx
,
2605 struct bt_list_head
*expressions
,
2606 struct bt_ctf_field_type
**float_decl
)
2610 struct ctf_node
*expression
;
2611 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2612 enum bt_ctf_byte_order byte_order
=
2613 bt_ctf_trace_get_byte_order(ctx
->trace
);
2617 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2618 struct ctf_node
*left
, *right
;
2620 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2621 struct ctf_node
, siblings
);
2622 right
= _BT_LIST_FIRST_ENTRY(
2623 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2626 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2631 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2632 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2633 _PERROR_DUP_ATTR("byte_order",
2634 "floating point number declaration");
2639 byte_order
= get_real_byte_order(ctx
, right
);
2640 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2641 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2646 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2647 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2649 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2650 _PERROR_DUP_ATTR("exp_dig",
2651 "floating point number declaration");
2656 if (right
->u
.unary_expression
.type
!=
2657 UNARY_UNSIGNED_CONSTANT
) {
2658 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2663 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2664 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2665 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2667 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2668 _PERROR_DUP_ATTR("mant_dig",
2669 "floating point number declaration");
2674 if (right
->u
.unary_expression
.type
!=
2675 UNARY_UNSIGNED_CONSTANT
) {
2676 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2681 mant_dig
= right
->u
.unary_expression
.u
.
2683 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2684 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2686 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2687 _PERROR_DUP_ATTR("align",
2688 "floating point number declaration");
2693 if (right
->u
.unary_expression
.type
!=
2694 UNARY_UNSIGNED_CONSTANT
) {
2695 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2700 alignment
= right
->u
.unary_expression
.u
.
2703 if (!is_align_valid(alignment
)) {
2704 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2709 _SET(&set
, _FLOAT_ALIGN_SET
);
2711 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2712 left
->u
.unary_expression
.u
.string
);
2716 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2717 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2722 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2723 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2728 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2729 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2730 /* Bit-packed alignment */
2733 /* Byte-packed alignment */
2734 alignment
= CHAR_BIT
;
2738 *float_decl
= bt_ctf_field_type_floating_point_create();
2741 "cannot create floating point number declaration");
2746 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2747 *float_decl
, exp_dig
);
2748 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2749 *float_decl
, mant_dig
);
2750 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2751 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2754 "cannot configure floating point number declaration");
2762 BT_PUT(*float_decl
);
2768 int visit_string_decl(struct ctx
*ctx
,
2769 struct bt_list_head
*expressions
,
2770 struct bt_ctf_field_type
**string_decl
)
2774 struct ctf_node
*expression
;
2775 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2777 *string_decl
= NULL
;
2779 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2780 struct ctf_node
*left
, *right
;
2782 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2783 struct ctf_node
, siblings
);
2784 right
= _BT_LIST_FIRST_ENTRY(
2785 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2788 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2793 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2796 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2797 _PERROR_DUP_ATTR("encoding",
2798 "string declaration");
2803 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2804 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2809 s_right
= concatenate_unary_strings(
2810 &expression
->u
.ctf_expression
.right
);
2812 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2817 if (!strcmp(s_right
, "UTF8") ||
2818 !strcmp(s_right
, "utf8") ||
2819 !strcmp(s_right
, "utf-8") ||
2820 !strcmp(s_right
, "UTF-8")) {
2821 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2822 } else if (!strcmp(s_right
, "ASCII") ||
2823 !strcmp(s_right
, "ascii")) {
2824 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2825 } else if (!strcmp(s_right
, "none")) {
2826 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2828 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2836 _SET(&set
, _STRING_ENCODING_SET
);
2838 _PWARNING("unknown attribute \"%s\" in string declaration",
2839 left
->u
.unary_expression
.u
.string
);
2843 *string_decl
= bt_ctf_field_type_string_create();
2844 if (!*string_decl
) {
2845 _PERROR("%s", "cannot create string declaration");
2850 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2852 _PERROR("%s", "cannot configure string declaration");
2860 BT_PUT(*string_decl
);
2866 int visit_type_specifier_list(struct ctx
*ctx
,
2867 struct ctf_node
*ts_list
,
2868 struct bt_ctf_field_type
**decl
)
2871 struct ctf_node
*first
, *node
;
2875 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2880 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2881 struct ctf_node
, siblings
);
2882 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2887 node
= first
->u
.type_specifier
.node
;
2889 switch (first
->u
.type_specifier
.type
) {
2890 case TYPESPEC_INTEGER
:
2891 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2898 case TYPESPEC_FLOATING_POINT
:
2899 ret
= visit_floating_point_number_decl(ctx
,
2900 &node
->u
.floating_point
.expressions
, decl
);
2906 case TYPESPEC_STRING
:
2907 ret
= visit_string_decl(ctx
,
2908 &node
->u
.string
.expressions
, decl
);
2914 case TYPESPEC_STRUCT
:
2915 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2916 &node
->u
._struct
.declaration_list
,
2917 node
->u
._struct
.has_body
,
2918 &node
->u
._struct
.min_align
, decl
);
2924 case TYPESPEC_VARIANT
:
2925 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2926 node
->u
.variant
.choice
,
2927 &node
->u
.variant
.declaration_list
,
2928 node
->u
.variant
.has_body
, decl
);
2935 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2936 node
->u
._enum
.container_type
,
2937 &node
->u
._enum
.enumerator_list
,
2938 node
->u
._enum
.has_body
, decl
);
2946 case TYPESPEC_SHORT
:
2949 case TYPESPEC_FLOAT
:
2950 case TYPESPEC_DOUBLE
:
2951 case TYPESPEC_SIGNED
:
2952 case TYPESPEC_UNSIGNED
:
2954 case TYPESPEC_COMPLEX
:
2955 case TYPESPEC_IMAGINARY
:
2956 case TYPESPEC_CONST
:
2957 case TYPESPEC_ID_TYPE
:
2958 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2965 _PERROR("unexpected node type: %d",
2966 (int) first
->u
.type_specifier
.type
);
2982 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2983 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2988 _BT_CTF_FIELD_TYPE_INIT(decl
);
2990 switch (node
->type
) {
2992 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2993 &node
->u
._typedef
.type_declarators
);
2996 "cannot add typedef in \"event\" declaration");
3000 case NODE_TYPEALIAS
:
3001 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3002 node
->u
.typealias
.alias
);
3004 _PERROR("%s", "cannot add typealias in \"event\" declaration");
3008 case NODE_CTF_EXPRESSION
:
3010 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3016 if (!strcmp(left
, "name")) {
3017 /* This is already known at this stage */
3018 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3019 _PERROR_DUP_ATTR("name", "event declaration");
3024 _SET(set
, _EVENT_NAME_SET
);
3025 } else if (!strcmp(left
, "id")) {
3028 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3029 _PERROR_DUP_ATTR("id", "event declaration");
3034 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3036 if (ret
|| id
< 0) {
3037 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3042 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3045 "cannot set event declaration's ID");
3049 _SET(set
, _EVENT_ID_SET
);
3050 } else if (!strcmp(left
, "stream_id")) {
3051 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3052 _PERROR_DUP_ATTR("stream_id",
3053 "event declaration");
3058 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3059 (uint64_t *) stream_id
);
3060 if (ret
|| *stream_id
< 0) {
3061 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3066 _SET(set
, _EVENT_STREAM_ID_SET
);
3067 } else if (!strcmp(left
, "context")) {
3068 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3069 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3074 ret
= visit_type_specifier_list(ctx
,
3075 _BT_LIST_FIRST_ENTRY(
3076 &node
->u
.ctf_expression
.right
,
3077 struct ctf_node
, siblings
),
3080 _PERROR("%s", "cannot create event context declaration");
3085 ret
= bt_ctf_event_class_set_context_type(
3089 _PERROR("%s", "cannot set event's context declaration");
3093 _SET(set
, _EVENT_CONTEXT_SET
);
3094 } else if (!strcmp(left
, "fields")) {
3095 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3096 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3101 ret
= visit_type_specifier_list(ctx
,
3102 _BT_LIST_FIRST_ENTRY(
3103 &node
->u
.ctf_expression
.right
,
3104 struct ctf_node
, siblings
),
3107 _PERROR("%s", "cannot create event payload declaration");
3112 ret
= bt_ctf_event_class_set_payload_type(
3116 _PERROR("%s", "cannot set event's payload declaration");
3120 _SET(set
, _EVENT_FIELDS_SET
);
3121 } else if (!strcmp(left
, "loglevel")) {
3122 uint64_t loglevel_value
;
3123 const char *loglevel_str
;
3124 struct bt_value
*value_obj
, *str_obj
;
3126 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3127 _PERROR_DUP_ATTR("loglevel",
3128 "event declaration");
3133 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3136 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3140 value_obj
= bt_value_integer_create_init(loglevel_value
);
3142 _PERROR("%s", "cannot allocate memory for loglevel value object");
3146 if (bt_ctf_event_class_set_attribute(event_class
,
3147 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3148 _PERROR("%s", "cannot set loglevel value");
3153 loglevel_str
= print_loglevel(loglevel_value
);
3155 str_obj
= bt_value_string_create_init(loglevel_str
);
3156 if (bt_ctf_event_class_set_attribute(event_class
,
3157 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3158 _PERROR("%s", "cannot set loglevel string");
3166 _SET(set
, _EVENT_LOGLEVEL_SET
);
3167 } else if (!strcmp(left
, "model.emf.uri")) {
3170 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3171 _PERROR_DUP_ATTR("model.emf.uri",
3172 "event declaration");
3177 right
= concatenate_unary_strings(
3178 &node
->u
.ctf_expression
.right
);
3180 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3185 // TODO: FIXME: set model EMF URI here
3188 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3190 _PWARNING("unknown attribute \"%s\" in event declaration",
3216 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3220 struct ctf_node
*iter
;
3221 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3223 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3224 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3228 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3233 if (!strcmp(left
, "name")) {
3234 name
= concatenate_unary_strings(
3235 &iter
->u
.ctf_expression
.right
);
3237 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3259 int reset_event_decl_types(struct ctx
*ctx
,
3260 struct bt_ctf_event_class
*event_class
)
3265 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3267 _PERROR("%s", "cannot set initial NULL event context");
3271 /* Event payload. */
3272 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3274 _PERROR("%s", "cannot set initial NULL event payload");
3282 int reset_stream_decl_types(struct ctx
*ctx
,
3283 struct bt_ctf_stream_class
*stream_class
)
3287 /* Packet context. */
3288 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3290 _PERROR("%s", "cannot set initial empty packet context");
3295 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3297 _PERROR("%s", "cannot set initial empty event header");
3301 /* Event context. */
3302 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3304 _PERROR("%s", "cannot set initial empty stream event context");
3312 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3315 struct bt_ctf_stream_class
*stream_class
;
3317 stream_class
= bt_ctf_stream_class_create(NULL
);
3318 if (!stream_class
) {
3319 _PERROR("%s", "cannot create stream class");
3324 * Set packet context, event header, and event context to NULL to
3325 * override the default ones.
3327 ret
= reset_stream_decl_types(ctx
, stream_class
);
3332 return stream_class
;
3335 BT_PUT(stream_class
);
3341 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3346 struct ctf_node
*iter
;
3347 int64_t stream_id
= -1;
3348 char *event_name
= NULL
;
3349 struct bt_ctf_event_class
*event_class
= NULL
;
3350 struct bt_ctf_event_class
*eevent_class
;
3351 struct bt_ctf_stream_class
*stream_class
= NULL
;
3352 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3353 bool pop_scope
= false;
3355 if (node
->visited
) {
3359 node
->visited
= TRUE
;
3360 event_name
= get_event_decl_name(ctx
, node
);
3363 "missing \"name\" attribute in event declaration");
3368 event_class
= bt_ctf_event_class_create(event_name
);
3371 * Unset context and fields to override the default ones.
3373 ret
= reset_event_decl_types(ctx
, event_class
);
3378 ret
= ctx_push_scope(ctx
);
3380 _PERROR("%s", "cannot push scope");
3386 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3387 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3394 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3396 struct bt_ctf_stream_class
*new_stream_class
;
3397 size_t stream_class_count
=
3398 g_hash_table_size(ctx
->stream_classes
) +
3399 bt_ctf_trace_get_stream_class_count(ctx
->trace
);
3402 * Allow missing stream_id if there is only a single
3405 switch (stream_class_count
) {
3407 /* Create implicit stream class if there's none */
3408 new_stream_class
= create_reset_stream_class(ctx
);
3409 if (!new_stream_class
) {
3414 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3416 _PERROR("%s", "cannot set stream class's ID");
3417 BT_PUT(new_stream_class
);
3423 /* Move reference to visitor's context */
3424 g_hash_table_insert(ctx
->stream_classes
,
3425 (gpointer
) stream_id
, new_stream_class
);
3426 new_stream_class
= NULL
;
3429 /* Single stream class: get its ID */
3430 if (g_hash_table_size(ctx
->stream_classes
) == 1) {
3431 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3432 stream_id
= (int64_t) keys
->data
;
3435 assert(bt_ctf_trace_get_stream_class_count(
3437 stream_class
= bt_ctf_trace_get_stream_class(
3439 assert(stream_class
);
3440 stream_id
= bt_ctf_stream_class_get_id(
3442 BT_PUT(stream_class
);
3446 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3452 assert(stream_id
>= 0);
3454 /* We have the stream ID now; get the stream class if found */
3455 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3456 (gpointer
) stream_id
);
3457 bt_get(stream_class
);
3458 if (!stream_class
) {
3459 stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3461 if (!stream_class
) {
3462 _PERROR("cannot find stream class with ID %" PRId64
,
3469 assert(stream_class
);
3471 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3472 /* Allow only one event without ID per stream */
3473 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3476 "missing \"id\" field in event declaration");
3482 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3484 _PERROR("%s", "cannot set event's ID");
3489 event_id
= bt_ctf_event_class_get_id(event_class
);
3491 _PERROR("%s", "cannot get event's ID");
3496 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3499 BT_PUT(eevent_class
);
3500 _PERROR("%s", "duplicate event with ID %" PRId64
" in same stream");
3505 eevent_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
,
3508 BT_PUT(eevent_class
);
3510 "duplicate event with name \"%s\" in same stream");
3515 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3516 BT_PUT(event_class
);
3518 _PERROR("%s", "cannot add event class to stream class");
3525 bt_put(event_class
);
3533 bt_put(stream_class
);
3538 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3539 struct bt_ctf_stream_class
*stream_class
, int *set
)
3543 _BT_CTF_FIELD_TYPE_INIT(decl
);
3545 switch (node
->type
) {
3547 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3548 &node
->u
._typedef
.type_declarators
);
3551 "cannot add typedef in \"stream\" declaration");
3555 case NODE_TYPEALIAS
:
3556 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3557 node
->u
.typealias
.alias
);
3559 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3563 case NODE_CTF_EXPRESSION
:
3565 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3571 if (!strcmp(left
, "id")) {
3575 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3576 _PERROR_DUP_ATTR("id", "stream declaration");
3581 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3583 if (ret
|| id
< 0) {
3584 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3589 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3592 _PERROR("duplicate stream with ID %" PRId64
,
3598 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3601 "cannot set stream declaration's ID");
3605 _SET(set
, _STREAM_ID_SET
);
3606 } else if (!strcmp(left
, "event.header")) {
3607 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3608 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3613 ret
= visit_type_specifier_list(ctx
,
3614 _BT_LIST_FIRST_ENTRY(
3615 &node
->u
.ctf_expression
.right
,
3616 struct ctf_node
, siblings
),
3619 _PERROR("%s", "cannot create event header declaration");
3625 ret
= bt_ctf_stream_class_set_event_header_type(
3626 stream_class
, decl
);
3629 _PERROR("%s", "cannot set stream's event header declaration");
3633 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3634 } else if (!strcmp(left
, "event.context")) {
3635 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3636 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3641 ret
= visit_type_specifier_list(ctx
,
3642 _BT_LIST_FIRST_ENTRY(
3643 &node
->u
.ctf_expression
.right
,
3644 struct ctf_node
, siblings
),
3647 _PERROR("%s", "cannot create stream event context declaration");
3653 ret
= bt_ctf_stream_class_set_event_context_type(
3654 stream_class
, decl
);
3657 _PERROR("%s", "cannot set stream's event context declaration");
3661 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3662 } else if (!strcmp(left
, "packet.context")) {
3663 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3664 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3669 ret
= visit_type_specifier_list(ctx
,
3670 _BT_LIST_FIRST_ENTRY(
3671 &node
->u
.ctf_expression
.right
,
3672 struct ctf_node
, siblings
),
3675 _PERROR("%s", "cannot create packet context declaration");
3681 ret
= bt_ctf_stream_class_set_packet_context_type(
3682 stream_class
, decl
);
3685 _PERROR("%s", "cannot set stream's packet context declaration");
3689 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3691 _PWARNING("unknown attribute \"%s\" in stream declaration",
3715 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3720 struct ctf_node
*iter
;
3721 struct bt_ctf_stream_class
*stream_class
= NULL
;
3722 struct bt_ctf_stream_class
*existing_stream_class
= NULL
;
3723 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3725 if (node
->visited
) {
3729 node
->visited
= TRUE
;
3730 stream_class
= create_reset_stream_class(ctx
);
3731 if (!stream_class
) {
3736 ret
= ctx_push_scope(ctx
);
3738 _PERROR("%s", "cannot push scope");
3742 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3743 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3752 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3753 /* Check that packet header has stream_id field */
3754 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3755 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3757 packet_header_decl
=
3758 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3759 if (!packet_header_decl
) {
3761 "cannot get trace packet header declaration");
3766 bt_ctf_field_type_structure_get_field_type_by_name(
3767 packet_header_decl
, "stream_id");
3768 BT_PUT(packet_header_decl
);
3769 if (!stream_id_decl
) {
3770 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3774 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3775 BT_PUT(stream_id_decl
);
3776 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3780 BT_PUT(stream_id_decl
);
3782 /* Allow only _one_ ID-less stream */
3783 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3785 "missing \"id\" field in stream declaration");
3790 /* Automatic ID: 0 */
3791 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3794 id
= bt_ctf_stream_class_get_id(stream_class
);
3796 _PERROR("wrong stream ID: %" PRId64
, id
);
3802 * Make sure that this stream class's ID is currently unique in
3805 if (g_hash_table_lookup(ctx
->stream_classes
, (gpointer
) id
)) {
3806 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3811 existing_stream_class
= bt_ctf_trace_get_stream_class_by_id(ctx
->trace
,
3813 if (existing_stream_class
) {
3814 _PERROR("a stream class with ID: %" PRId64
" already exists in the trace", id
);
3819 /* Move reference to visitor's context */
3820 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3822 stream_class
= NULL
;
3826 bt_put(stream_class
);
3829 bt_put(existing_stream_class
);
3834 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3838 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3840 switch (node
->type
) {
3842 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3843 &node
->u
._typedef
.type_declarators
);
3846 "cannot add typedef in \"trace\" declaration");
3850 case NODE_TYPEALIAS
:
3851 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3852 node
->u
.typealias
.alias
);
3855 "cannot add typealias in \"trace\" declaration");
3859 case NODE_CTF_EXPRESSION
:
3861 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3867 if (!strcmp(left
, "major")) {
3868 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3869 _PERROR_DUP_ATTR("major", "trace declaration");
3874 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3877 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3882 _SET(set
, _TRACE_MAJOR_SET
);
3883 } else if (!strcmp(left
, "minor")) {
3884 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3885 _PERROR_DUP_ATTR("minor", "trace declaration");
3890 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3893 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3898 _SET(set
, _TRACE_MINOR_SET
);
3899 } else if (!strcmp(left
, "uuid")) {
3900 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3901 _PERROR_DUP_ATTR("uuid", "trace declaration");
3906 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3910 "invalid trace declaration's UUID");
3914 _SET(set
, _TRACE_UUID_SET
);
3915 } else if (!strcmp(left
, "byte_order")) {
3916 /* Native byte order is already known at this stage */
3917 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3918 _PERROR_DUP_ATTR("byte_order",
3919 "trace declaration");
3924 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3925 } else if (!strcmp(left
, "packet.header")) {
3926 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3927 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3932 ret
= visit_type_specifier_list(ctx
,
3933 _BT_LIST_FIRST_ENTRY(
3934 &node
->u
.ctf_expression
.right
,
3935 struct ctf_node
, siblings
),
3936 &packet_header_decl
);
3938 _PERROR("%s", "cannot create packet header declaration");
3942 assert(packet_header_decl
);
3943 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3944 packet_header_decl
);
3945 BT_PUT(packet_header_decl
);
3947 _PERROR("%s", "cannot set trace declaration's packet header declaration");
3951 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3953 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3961 _PERROR("%s", "unknown expression in trace declaration");
3970 BT_PUT(packet_header_decl
);
3976 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3980 struct ctf_node
*iter
;
3981 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3983 if (node
->visited
) {
3987 node
->visited
= TRUE
;
3989 if (ctx
->is_trace_visited
) {
3990 _PERROR("%s", "duplicate \"trace\" block");
3995 ret
= ctx_push_scope(ctx
);
3997 _PERROR("%s", "cannot push scope");
4001 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
4002 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
4011 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
4013 "missing \"major\" attribute in trace declaration");
4018 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
4020 "missing \"minor\" attribute in trace declaration");
4025 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4026 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4031 ctx
->is_trace_visited
= TRUE
;
4041 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
4045 struct ctf_node
*entry_node
;
4046 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
4048 if (node
->visited
) {
4052 node
->visited
= TRUE
;
4054 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4055 struct bt_list_head
*right_head
=
4056 &entry_node
->u
.ctf_expression
.right
;
4058 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4059 _PERROR("%s", "wrong expression in environment entry");
4064 left
= concatenate_unary_strings(
4065 &entry_node
->u
.ctf_expression
.left
);
4067 _PERROR("%s", "cannot get environment entry name");
4072 if (is_unary_string(right_head
)) {
4073 char *right
= concatenate_unary_strings(right_head
);
4076 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4082 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4083 ret
= bt_ctf_trace_set_environment_field_string(
4084 ctx
->trace
, left
, right
);
4088 _PERROR("environment: cannot add entry \"%s\" to trace",
4092 } else if (is_unary_unsigned(right_head
) ||
4093 is_unary_signed(right_head
)) {
4096 if (is_unary_unsigned(right_head
)) {
4097 ret
= get_unary_unsigned(right_head
,
4100 ret
= get_unary_signed(right_head
, &v
);
4103 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4109 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4110 ret
= bt_ctf_trace_set_environment_field_integer(
4111 ctx
->trace
, left
, v
);
4113 _PERROR("environment: cannot add entry \"%s\" to trace",
4118 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4136 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4141 struct ctf_node
*node
;
4142 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4144 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4145 if (node
->type
== NODE_CTF_EXPRESSION
) {
4146 struct ctf_node
*right_node
;
4148 left
= concatenate_unary_strings(
4149 &node
->u
.ctf_expression
.left
);
4155 if (!strcmp(left
, "byte_order")) {
4156 enum bt_ctf_byte_order bo
;
4158 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4159 _PERROR_DUP_ATTR("byte_order",
4160 "trace declaration");
4165 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4166 right_node
= _BT_LIST_FIRST_ENTRY(
4167 &node
->u
.ctf_expression
.right
,
4168 struct ctf_node
, siblings
);
4169 bo
= byte_order_from_unary_expr(ctx
->efd
,
4171 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4172 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4175 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4176 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4182 ret
= bt_ctf_trace_set_native_byte_order(
4185 _PERROR("cannot set trace's byte order (%d)",
4196 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4197 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4211 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4212 struct bt_ctf_clock_class
*clock
, int *set
)
4217 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4222 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4228 if (!strcmp(left
, "name")) {
4231 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4232 _PERROR_DUP_ATTR("name", "clock class declaration");
4237 right
= concatenate_unary_strings(
4238 &entry_node
->u
.ctf_expression
.right
);
4240 _PERROR("%s", "unexpected unary expression for clock class declaration's \"name\" attribute");
4245 ret
= bt_ctf_clock_class_set_name(clock
, right
);
4247 _PERROR("%s", "cannot set clock class's name");
4253 _SET(set
, _CLOCK_NAME_SET
);
4254 } else if (!strcmp(left
, "uuid")) {
4255 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4257 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4258 _PERROR_DUP_ATTR("uuid", "clock class declaration");
4263 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4265 _PERROR("%s", "invalid clock class UUID");
4269 ret
= bt_ctf_clock_class_set_uuid(clock
, uuid
);
4271 _PERROR("%s", "cannot set clock class's UUID");
4275 _SET(set
, _CLOCK_UUID_SET
);
4276 } else if (!strcmp(left
, "description")) {
4279 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4280 _PERROR_DUP_ATTR("description", "clock class declaration");
4285 right
= concatenate_unary_strings(
4286 &entry_node
->u
.ctf_expression
.right
);
4288 _PERROR("%s", "unexpected unary expression for clock class's \"description\" attribute");
4293 ret
= bt_ctf_clock_class_set_description(clock
, right
);
4295 _PERROR("%s", "cannot set clock class's description");
4301 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4302 } else if (!strcmp(left
, "freq")) {
4305 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4306 _PERROR_DUP_ATTR("freq", "clock class declaration");
4311 ret
= get_unary_unsigned(
4312 &entry_node
->u
.ctf_expression
.right
, &freq
);
4314 _PERROR("%s", "unexpected unary expression for clock class declaration's \"freq\" attribute");
4319 ret
= bt_ctf_clock_class_set_frequency(clock
, freq
);
4321 _PERROR("%s", "cannot set clock class's frequency");
4325 _SET(set
, _CLOCK_FREQ_SET
);
4326 } else if (!strcmp(left
, "precision")) {
4329 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4330 _PERROR_DUP_ATTR("precision", "clock class declaration");
4335 ret
= get_unary_unsigned(
4336 &entry_node
->u
.ctf_expression
.right
, &precision
);
4338 _PERROR("%s", "unexpected unary expression for clock class declaration's \"precision\" attribute");
4343 ret
= bt_ctf_clock_class_set_precision(clock
, precision
);
4345 _PERROR("%s", "cannot set clock class's precision");
4349 _SET(set
, _CLOCK_PRECISION_SET
);
4350 } else if (!strcmp(left
, "offset_s")) {
4353 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4354 _PERROR_DUP_ATTR("offset_s", "clock class declaration");
4359 ret
= get_unary_unsigned(
4360 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4362 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset_s\" attribute");
4367 ret
= bt_ctf_clock_class_set_offset_s(clock
, offset_s
);
4369 _PERROR("%s", "cannot set clock class's offset in seconds");
4373 _SET(set
, _CLOCK_OFFSET_S_SET
);
4374 } else if (!strcmp(left
, "offset")) {
4377 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4378 _PERROR_DUP_ATTR("offset", "clock class declaration");
4383 ret
= get_unary_unsigned(
4384 &entry_node
->u
.ctf_expression
.right
, &offset
);
4386 _PERROR("%s", "unexpected unary expression for clock class declaration's \"offset\" attribute");
4391 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset
);
4393 _PERROR("%s", "cannot set clock class's offset in cycles");
4397 _SET(set
, _CLOCK_OFFSET_SET
);
4398 } else if (!strcmp(left
, "absolute")) {
4399 struct ctf_node
*right
;
4401 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4402 _PERROR_DUP_ATTR("absolute", "clock class declaration");
4407 right
= _BT_LIST_FIRST_ENTRY(
4408 &entry_node
->u
.ctf_expression
.right
,
4409 struct ctf_node
, siblings
);
4410 ret
= get_boolean(ctx
->efd
, right
);
4412 _PERROR("%s", "unexpected unary expression for clock class declaration's \"absolute\" attribute");
4417 ret
= bt_ctf_clock_class_set_is_absolute(clock
, ret
);
4419 _PERROR("%s", "cannot set clock class's absolute option");
4423 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4425 _PWARNING("unknown attribute \"%s\" in clock class declaration",
4441 uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4446 if (frequency
== 1000000000ULL) {
4449 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4456 int apply_clock_class_offset(struct ctx
*ctx
, struct bt_ctf_clock_class
*clock
)
4460 int64_t offset_cycles
;
4462 freq
= bt_ctf_clock_class_get_frequency(clock
);
4463 if (freq
== -1ULL) {
4464 _PERROR("%s", "cannot get clock class frequency");
4469 ret
= bt_ctf_clock_class_get_offset_cycles(clock
, &offset_cycles
);
4471 _PERROR("%s", "cannot get offset in cycles");
4476 offset_cycles
+= cycles_from_ns(freq
, ctx
->clock_class_offset_ns
);
4477 ret
= bt_ctf_clock_class_set_offset_cycles(clock
, offset_cycles
);
4484 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4488 struct bt_ctf_clock_class
*clock
;
4489 struct ctf_node
*entry_node
;
4490 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4492 if (clock_node
->visited
) {
4496 clock_node
->visited
= TRUE
;
4497 clock
= bt_ctf_clock_class_create(NULL
);
4499 _PERROR("%s", "cannot create clock");
4504 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4505 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4511 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4513 "missing \"name\" attribute in clock class declaration");
4518 if (bt_ctf_trace_get_clock_class_count(ctx
->trace
) != 0) {
4519 _PERROR("%s", "only CTF traces with a single clock class declaration are supported as of this version");
4524 ret
= apply_clock_class_offset(ctx
, clock
);
4526 _PERROR("%s", "cannot apply clock class offset ");
4530 ret
= bt_ctf_trace_add_clock_class(ctx
->trace
, clock
);
4532 _PERROR("%s", "cannot add clock class to trace");
4543 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4547 if (root_decl_node
->visited
) {
4551 root_decl_node
->visited
= TRUE
;
4553 switch (root_decl_node
->type
) {
4555 ret
= visit_typedef(ctx
,
4556 root_decl_node
->u
._typedef
.type_specifier_list
,
4557 &root_decl_node
->u
._typedef
.type_declarators
);
4559 _PERROR("%s", "cannot add typedef in root scope");
4563 case NODE_TYPEALIAS
:
4564 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4565 root_decl_node
->u
.typealias
.alias
);
4567 _PERROR("%s", "cannot add typealias in root scope");
4571 case NODE_TYPE_SPECIFIER_LIST
:
4573 _BT_CTF_FIELD_TYPE_INIT(decl
);
4576 * Just add the type specifier to the root
4577 * declaration scope. Put local reference.
4579 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4598 int move_ctx_stream_classes_to_trace(struct ctx
*ctx
)
4601 GHashTableIter iter
;
4602 gpointer key
, stream_class
;
4604 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4606 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4607 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4610 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4611 _PERROR("cannot add stream class %" PRId64
" to trace",
4617 g_hash_table_remove_all(ctx
->stream_classes
);
4624 struct ctf_visitor_generate_ir
*ctf_visitor_generate_ir_create(FILE *efd
,
4625 uint64_t clock_class_offset_ns
)
4628 struct ctx
*ctx
= NULL
;
4629 struct bt_ctf_trace
*trace
;
4631 trace
= bt_ctf_trace_create();
4633 _FPERROR(efd
, "%s", "cannot create trace");
4637 /* Set packet header to NULL to override the default one */
4638 ret
= bt_ctf_trace_set_packet_header_type(trace
, NULL
);
4641 "cannot set initial, empty packet header structure");
4645 /* Create visitor's context */
4646 ctx
= ctx_create(trace
, efd
, clock_class_offset_ns
);
4648 _FPERROR(efd
, "%s", "cannot create visitor context");
4661 return (void *) ctx
;
4665 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4667 ctx_destroy((void *) visitor
);
4671 struct bt_ctf_trace
*ctf_visitor_generate_ir_get_trace(
4672 struct ctf_visitor_generate_ir
*visitor
)
4674 struct ctx
*ctx
= (void *) visitor
;
4678 return bt_get(ctx
->trace
);
4682 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*visitor
,
4683 struct ctf_node
*node
)
4686 struct ctx
*ctx
= (void *) visitor
;
4688 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4690 switch (node
->type
) {
4693 struct ctf_node
*iter
;
4694 int got_trace_decl
= FALSE
;
4695 int found_callsite
= FALSE
;
4698 * The first thing we need is the native byte order of
4699 * the trace block, because early type aliases can have
4700 * a `byte_order` attribute set to `native`. If we don't
4701 * have the native byte order yet, and we don't have any
4702 * trace block yet, then fail with EINCOMPLETE.
4704 if (ctx
->trace_bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4705 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4706 if (got_trace_decl
) {
4707 _PERROR("%s", "duplicate trace declaration");
4712 ret
= set_trace_byte_order(ctx
, iter
);
4714 _PERROR("cannot set trace's native byte order (%d)",
4719 got_trace_decl
= TRUE
;
4722 if (!got_trace_decl
) {
4728 assert(ctx
->trace_bo
== BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
||
4729 ctx
->trace_bo
== BT_CTF_BYTE_ORDER_BIG_ENDIAN
);
4730 assert(ctx
->current_scope
&&
4731 ctx
->current_scope
->parent_scope
== NULL
);
4734 * Visit clocks first since any early integer can be mapped
4737 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4738 ret
= visit_clock_decl(ctx
, iter
);
4740 _PERROR("error while visiting clock class declaration (%d)",
4746 assert(ctx
->current_scope
&&
4747 ctx
->current_scope
->parent_scope
== NULL
);
4750 * Visit root declarations next, as they can be used by any
4753 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4755 ret
= visit_root_decl(ctx
, iter
);
4757 _PERROR("error while visiting root declaration (%d)",
4763 assert(ctx
->current_scope
&&
4764 ctx
->current_scope
->parent_scope
== NULL
);
4766 /* Callsite blocks are not supported */
4767 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4768 found_callsite
= TRUE
;
4772 assert(ctx
->current_scope
&&
4773 ctx
->current_scope
->parent_scope
== NULL
);
4775 if (found_callsite
) {
4776 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4780 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4781 ret
= visit_env(ctx
, iter
);
4783 _PERROR("error while visiting environment block (%d)",
4789 assert(ctx
->current_scope
&&
4790 ctx
->current_scope
->parent_scope
== NULL
);
4793 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4794 ret
= visit_trace_decl(ctx
, iter
);
4796 _PERROR("%s", "error while visiting trace declaration");
4801 assert(ctx
->current_scope
&&
4802 ctx
->current_scope
->parent_scope
== NULL
);
4805 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4806 ret
= visit_stream_decl(ctx
, iter
);
4808 _PERROR("%s", "error while visiting stream declaration");
4813 assert(ctx
->current_scope
&&
4814 ctx
->current_scope
->parent_scope
== NULL
);
4817 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4818 ret
= visit_event_decl(ctx
, iter
);
4820 _PERROR("%s", "error while visiting event declaration");
4825 assert(ctx
->current_scope
&&
4826 ctx
->current_scope
->parent_scope
== NULL
);
4830 _PERROR("cannot decode node type: %d", (int) node
->type
);
4835 /* Move decoded stream classes to trace, if any */
4836 ret
= move_ctx_stream_classes_to_trace(ctx
);
4838 _PERROR("%s", "cannot move stream classes to trace");