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/ctf/metadata.h>
40 #include <babeltrace/compat/uuid.h>
41 #include <babeltrace/endian.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/ctf/events-internal.h>
44 #include <babeltrace/ctf-ir/trace.h>
45 #include <babeltrace/ctf-ir/stream-class.h>
46 #include <babeltrace/ctf-ir/event.h>
47 #include <babeltrace/ctf-ir/event-class.h>
48 #include <babeltrace/ctf-ir/field-types.h>
49 #include <babeltrace/ctf-ir/field-types-internal.h>
50 #include <babeltrace/ctf-ir/clock.h>
56 /* Bit value (left shift) */
57 #define _BV(_val) (1 << (_val))
59 /* Bit is set in a set of bits */
60 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
62 /* Set bit in a set of bits */
63 #define _SET(_set, _mask) (*(_set) |= (_mask))
65 /* Bits for verifying existing attributes in various declarations */
67 _CLOCK_NAME_SET
= _BV(0),
68 _CLOCK_UUID_SET
= _BV(1),
69 _CLOCK_FREQ_SET
= _BV(2),
70 _CLOCK_PRECISION_SET
= _BV(3),
71 _CLOCK_OFFSET_S_SET
= _BV(4),
72 _CLOCK_OFFSET_SET
= _BV(5),
73 _CLOCK_ABSOLUTE_SET
= _BV(6),
74 _CLOCK_DESCRIPTION_SET
= _BV(7),
78 _INTEGER_ALIGN_SET
= _BV(0),
79 _INTEGER_SIZE_SET
= _BV(1),
80 _INTEGER_BASE_SET
= _BV(2),
81 _INTEGER_ENCODING_SET
= _BV(3),
82 _INTEGER_BYTE_ORDER_SET
= _BV(4),
83 _INTEGER_SIGNED_SET
= _BV(5),
84 _INTEGER_MAP_SET
= _BV(6),
88 _FLOAT_ALIGN_SET
= _BV(0),
89 _FLOAT_MANT_DIG_SET
= _BV(1),
90 _FLOAT_EXP_DIG_SET
= _BV(2),
91 _FLOAT_BYTE_ORDER_SET
= _BV(3),
95 _STRING_ENCODING_SET
= _BV(0),
99 _TRACE_MINOR_SET
= _BV(0),
100 _TRACE_MAJOR_SET
= _BV(1),
101 _TRACE_BYTE_ORDER_SET
= _BV(2),
102 _TRACE_UUID_SET
= _BV(3),
103 _TRACE_PACKET_HEADER_SET
= _BV(4),
107 _STREAM_ID_SET
= _BV(0),
108 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
109 _STREAM_EVENT_HEADER_SET
= _BV(2),
110 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
114 _EVENT_NAME_SET
= _BV(0),
115 _EVENT_ID_SET
= _BV(1),
116 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
117 _EVENT_STREAM_ID_SET
= _BV(3),
118 _EVENT_LOGLEVEL_SET
= _BV(4),
119 _EVENT_CONTEXT_SET
= _BV(5),
120 _EVENT_FIELDS_SET
= _BV(6),
128 LOGLEVEL_WARNING
= 4,
131 LOGLEVEL_DEBUG_SYSTEM
= 7,
132 LOGLEVEL_DEBUG_PROGRAM
= 8,
133 LOGLEVEL_DEBUG_PROCESS
= 9,
134 LOGLEVEL_DEBUG_MODULE
= 10,
135 LOGLEVEL_DEBUG_UNIT
= 11,
136 LOGLEVEL_DEBUG_FUNCTION
= 12,
137 LOGLEVEL_DEBUG_LINE
= 13,
142 /* Prefixes of type aliases */
143 #define _PREFIX_ALIAS 'a'
144 #define _PREFIX_ENUM 'e'
145 #define _PREFIX_STRUCT 's'
146 #define _PREFIX_VARIANT 'v'
148 /* First entry in a BT list */
149 #define _BT_LIST_FIRST_ENTRY(_ptr, _type, _member) \
150 bt_list_entry((_ptr)->next, _type, _member)
152 #define _BT_CTF_FIELD_TYPE_INIT(_name) struct bt_ctf_field_type *_name = NULL;
154 /* Error printing wrappers */
155 #define _PERROR(_fmt, ...) \
157 fprintf(ctx->efd, "[error] %s: " _fmt "\n", \
158 __func__, __VA_ARGS__); \
161 #define _PWARNING(_fmt, ...) \
163 fprintf(ctx->efd, "[warning] %s: " _fmt "\n", \
164 __func__, __VA_ARGS__); \
167 #define _FPERROR(_stream, _fmt, ...) \
169 fprintf(_stream, "[error] %s: " _fmt "\n", \
170 __func__, __VA_ARGS__); \
173 #define _FPWARNING(_stream, _fmt, ...) \
175 fprintf(_stream, "[warning] %s: " _fmt "\n", \
176 __func__, __VA_ARGS__); \
179 #define _PERROR_DUP_ATTR(_attr, _entity) \
182 "[error] %s: duplicate attribute \"" \
183 _attr "\" in " _entity "\n", __func__); \
187 * Declaration scope of a visitor context. This represents a TSDL
188 * lexical scope, so that aliases and named structures, variants,
189 * and enumerations may be registered and looked up hierarchically.
191 struct ctx_decl_scope
{
193 * Alias name to field type.
195 * GQuark -> struct bt_ctf_field_type *
197 GHashTable
*decl_map
;
199 /* Parent scope; NULL if this is the root declaration scope */
200 struct ctx_decl_scope
*parent_scope
;
207 /* Trace being filled (weak ref.) */
208 struct bt_ctf_trace
*trace
;
210 /* Error stream to use during visit */
213 /* Current declaration scope (top of the stack) */
214 struct ctx_decl_scope
*current_scope
;
216 /* 1 if trace declaration is visited */
217 int is_trace_visited
;
219 /* Trace attributes */
220 uint64_t trace_major
;
221 uint64_t trace_minor
;
222 unsigned char trace_uuid
[BABELTRACE_UUID_LEN
];
225 * Stream IDs to stream classes.
227 * int64_t -> struct bt_ctf_stream_class *
229 GHashTable
*stream_classes
;
233 const char *loglevel_str
[] = {
234 [ LOGLEVEL_EMERG
] = "TRACE_EMERG",
235 [ LOGLEVEL_ALERT
] = "TRACE_ALERT",
236 [ LOGLEVEL_CRIT
] = "TRACE_CRIT",
237 [ LOGLEVEL_ERR
] = "TRACE_ERR",
238 [ LOGLEVEL_WARNING
] = "TRACE_WARNING",
239 [ LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
240 [ LOGLEVEL_INFO
] = "TRACE_INFO",
241 [ LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
242 [ LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
243 [ LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
244 [ LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
245 [ LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
246 [ LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
247 [ LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
248 [ LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
252 const char *print_loglevel(int64_t value
)
257 if (value
>= _NR_LOGLEVELS
) {
258 return "<<UNKNOWN>>";
260 return loglevel_str
[value
];
264 * Creates a new declaration scope.
266 * @param par_scope Parent scope (NULL if creating a root scope)
267 * @returns New declaration scope, or NULL on error
270 struct ctx_decl_scope
*ctx_decl_scope_create(struct ctx_decl_scope
*par_scope
)
272 struct ctx_decl_scope
*scope
;
274 scope
= g_new(struct ctx_decl_scope
, 1);
279 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
280 NULL
, (GDestroyNotify
) bt_ctf_field_type_put
);
281 scope
->parent_scope
= par_scope
;
288 * Destroys a declaration scope.
290 * This function does not destroy the parent scope.
292 * @param scope Scope to destroy
295 void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
301 g_hash_table_destroy(scope
->decl_map
);
309 * Returns the GQuark of a prefixed alias.
311 * @param prefix Prefix character
313 * @returns Associated GQuark, or 0 on error
316 GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
322 /* Prefix character + original string + '\0' */
323 char *prname
= g_new(char, strlen(name
) + 2);
328 sprintf(prname
, "%c%s", prefix
, name
);
329 qname
= g_quark_from_string(prname
);
337 * Looks up a prefixed type alias within a declaration scope.
339 * @param scope Declaration scope
340 * @param prefix Prefix character
341 * @param name Alias name
342 * @param level Number of levels to dig (-1 means infinite)
343 * @returns Declaration, or NULL if not found
346 struct bt_ctf_field_type
*ctx_decl_scope_lookup_prefix_alias(
347 struct ctx_decl_scope
*scope
, char prefix
,
348 const char *name
, int levels
)
352 _BT_CTF_FIELD_TYPE_INIT(decl
);
353 struct ctx_decl_scope
*cur_scope
= scope
;
357 qname
= get_prefixed_named_quark(prefix
, name
);
366 while (cur_scope
&& cur_levels
< levels
) {
367 decl
= g_hash_table_lookup(cur_scope
->decl_map
,
368 (gconstpointer
) (unsigned long) qname
);
370 /* Caller's reference */
375 cur_scope
= cur_scope
->parent_scope
;
386 * Looks up a type alias within a declaration scope.
388 * @param scope Declaration scope
389 * @param name Alias name
390 * @param level Number of levels to dig (-1 means infinite)
391 * @returns Declaration, or NULL if not found
394 struct bt_ctf_field_type
*ctx_decl_scope_lookup_alias(
395 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
397 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
,
402 * Looks up an enumeration within a declaration scope.
404 * @param scope Declaration scope
405 * @param name Enumeration name
406 * @param level Number of levels to dig (-1 means infinite)
407 * @returns Declaration, or NULL if not found
410 struct bt_ctf_field_type
*ctx_decl_scope_lookup_enum(
411 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
413 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
,
418 * Looks up a structure within a declaration scope.
420 * @param scope Declaration scope
421 * @param name Structure name
422 * @param level Number of levels to dig (-1 means infinite)
423 * @returns Declaration, or NULL if not found
426 struct bt_ctf_field_type
*ctx_decl_scope_lookup_struct(
427 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
429 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
,
434 * Looks up a variant within a declaration scope.
436 * @param scope Declaration scope
437 * @param name Variant name
438 * @param level Number of levels to dig (-1 means infinite)
439 * @returns Declaration, or NULL if not found
442 struct bt_ctf_field_type
*ctx_decl_scope_lookup_variant(
443 struct ctx_decl_scope
*scope
, const char *name
, int levels
)
445 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
,
450 * Registers a prefixed type alias within a declaration scope.
452 * @param scope Declaration scope
453 * @param prefix Prefix character
454 * @param name Alias name (non-NULL)
455 * @param decl Declaration to register
456 * @returns 0 if registration went okay, negative value otherwise
459 int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
,
460 char prefix
, const char *name
, struct bt_ctf_field_type
*decl
)
464 _BT_CTF_FIELD_TYPE_INIT(edecl
);
469 qname
= get_prefixed_named_quark(prefix
, name
);
475 /* Make sure alias does not exist in local scope */
476 edecl
= ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1);
483 g_hash_table_insert(scope
->decl_map
,
484 (gpointer
) (unsigned long) qname
, decl
);
486 /* Hash table's reference */
496 * Registers a type alias within a declaration scope.
498 * @param scope Declaration scope
499 * @param name Alias name (non-NULL)
500 * @param decl Declaration to register
501 * @returns 0 if registration went okay, negative value otherwise
504 int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
,
505 const char *name
, struct bt_ctf_field_type
*decl
)
507 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
,
512 * Registers an enumeration declaration within a declaration scope.
514 * @param scope Declaration scope
515 * @param name Enumeration name (non-NULL)
516 * @param decl Enumeration declaration to register
517 * @returns 0 if registration went okay, negative value otherwise
520 int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
,
521 const char *name
, struct bt_ctf_field_type
*decl
)
523 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
,
528 * Registers a structure declaration within a declaration scope.
530 * @param scope Declaration scope
531 * @param name Structure name (non-NULL)
532 * @param decl Structure declaration to register
533 * @returns 0 if registration went okay, negative value otherwise
536 int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
,
537 const char *name
, struct bt_ctf_field_type
*decl
)
539 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
,
544 * Registers a variant declaration within a declaration scope.
546 * @param scope Declaration scope
547 * @param name Variant name (non-NULL)
548 * @param decl Variant declaration to register
549 * @returns 0 if registration went okay, negative value otherwise
552 int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
,
553 const char *name
, struct bt_ctf_field_type
*decl
)
555 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
,
560 * Creates a new visitor context.
562 * @param trace Associated trace
563 * @param efd Error stream
564 * @returns New visitor context, or NULL on error
567 struct ctx
*ctx_create(struct bt_ctf_trace
*trace
, FILE *efd
)
569 struct ctx
*ctx
= NULL
;
570 struct ctx_decl_scope
*scope
= NULL
;
572 ctx
= g_new(struct ctx
, 1);
577 /* Root declaration scope */
578 scope
= ctx_decl_scope_create(NULL
);
583 ctx
->stream_classes
= g_hash_table_new_full(g_direct_hash
,
584 g_direct_equal
, NULL
, (GDestroyNotify
) bt_put
);
585 if (!ctx
->stream_classes
) {
591 ctx
->current_scope
= scope
;
592 ctx
->is_trace_visited
= FALSE
;
598 ctx_decl_scope_destroy(scope
);
604 * Destroys a visitor context.
606 * @param ctx Visitor context to destroy
609 void ctx_destroy(struct ctx
*ctx
)
611 struct ctx_decl_scope
*scope
;
613 * Destroy all scopes, from current one to the root scope.
620 scope
= ctx
->current_scope
;
623 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
625 ctx_decl_scope_destroy(scope
);
626 scope
= parent_scope
;
629 g_hash_table_destroy(ctx
->stream_classes
);
637 * Pushes a new declaration scope on top of a visitor context's
638 * declaration scope stack.
640 * @param ctx Visitor context
641 * @returns 0 on success, or a negative value on error
644 int ctx_push_scope(struct ctx
*ctx
)
647 struct ctx_decl_scope
*new_scope
;
650 new_scope
= ctx_decl_scope_create(ctx
->current_scope
);
656 ctx
->current_scope
= new_scope
;
663 void ctx_pop_scope(struct ctx
*ctx
)
665 struct ctx_decl_scope
*parent_scope
= NULL
;
669 if (!ctx
->current_scope
) {
673 parent_scope
= ctx
->current_scope
->parent_scope
;
674 ctx_decl_scope_destroy(ctx
->current_scope
);
675 ctx
->current_scope
= parent_scope
;
682 int visit_type_specifier_list(struct ctx
*ctx
, struct ctf_node
*ts_list
,
683 struct bt_ctf_field_type
**decl
);
686 int is_unary_string(struct bt_list_head
*head
)
689 struct ctf_node
*node
;
691 bt_list_for_each_entry(node
, head
, siblings
) {
692 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
696 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
705 char *concatenate_unary_strings(struct bt_list_head
*head
)
709 struct ctf_node
*node
;
711 str
= g_string_new(NULL
);
713 bt_list_for_each_entry(node
, head
, siblings
) {
717 node
->type
!= NODE_UNARY_EXPRESSION
||
718 node
->u
.unary_expression
.type
!= UNARY_STRING
||
721 node
->u
.unary_expression
.link
!=
729 switch (node
->u
.unary_expression
.link
) {
731 g_string_append(str
, ".");
733 case UNARY_ARROWLINK
:
734 g_string_append(str
, "->");
736 case UNARY_DOTDOTDOT
:
737 g_string_append(str
, "...");
743 src_string
= node
->u
.unary_expression
.u
.string
;
744 g_string_append(str
, src_string
);
748 /* Destroys the container, returns the underlying string */
749 return g_string_free(str
, FALSE
);
752 /* This always returns NULL */
753 return g_string_free(str
, TRUE
);
757 const char *get_map_clock_name_value(struct bt_list_head
*head
)
760 struct ctf_node
*node
;
761 const char *name
= NULL
;
763 bt_list_for_each_entry(node
, head
, siblings
) {
765 int uexpr_type
= node
->u
.unary_expression
.type
;
766 int uexpr_link
= node
->u
.unary_expression
.link
;
767 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
768 uexpr_type
!= UNARY_STRING
||
769 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
774 /* Needs to be chained with . */
775 switch (node
->u
.unary_expression
.link
) {
778 case UNARY_ARROWLINK
:
779 case UNARY_DOTDOTDOT
:
785 src_string
= node
->u
.unary_expression
.u
.string
;
789 if (strcmp("clock", src_string
)) {
797 if (strcmp("value", src_string
)) {
802 /* Extra identifier, unknown */
816 int is_unary_unsigned(struct bt_list_head
*head
)
819 struct ctf_node
*node
;
821 bt_list_for_each_entry(node
, head
, siblings
) {
822 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
826 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
835 int get_unary_unsigned(struct bt_list_head
*head
, uint64_t *value
)
839 struct ctf_node
*node
;
841 bt_list_for_each_entry(node
, head
, siblings
) {
842 int uexpr_type
= node
->u
.unary_expression
.type
;
843 int uexpr_link
= node
->u
.unary_expression
.link
;
844 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
845 uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
846 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
852 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
861 int is_unary_signed(struct bt_list_head
*head
)
864 struct ctf_node
*node
;
866 bt_list_for_each_entry(node
, head
, siblings
) {
867 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
871 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
880 int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
884 struct ctf_node
*node
;
886 bt_list_for_each_entry(node
, head
, siblings
) {
887 int uexpr_type
= node
->u
.unary_expression
.type
;
888 int uexpr_link
= node
->u
.unary_expression
.link
;
889 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
890 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
) ||
891 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&&
892 uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
893 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
899 switch (node
->u
.unary_expression
.type
) {
900 case UNARY_UNSIGNED_CONSTANT
:
902 node
->u
.unary_expression
.u
.unsigned_constant
;
904 case UNARY_SIGNED_CONSTANT
:
905 *value
= node
->u
.unary_expression
.u
.signed_constant
;
920 int get_unary_uuid(struct bt_list_head
*head
, unsigned char *uuid
)
924 struct ctf_node
*node
;
926 bt_list_for_each_entry(node
, head
, siblings
) {
927 int uexpr_type
= node
->u
.unary_expression
.type
;
928 int uexpr_link
= node
->u
.unary_expression
.link
;
929 const char *src_string
;
931 if (node
->type
!= NODE_UNARY_EXPRESSION
||
932 uexpr_type
!= UNARY_STRING
||
933 uexpr_link
!= UNARY_LINK_UNKNOWN
||
939 src_string
= node
->u
.unary_expression
.u
.string
;
940 ret
= bt_uuid_parse(src_string
, uuid
);
951 int get_boolean(FILE *efd
, struct ctf_node
*unary_expr
)
955 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
956 _FPERROR(efd
, "%s", "expecting unary expression");
961 switch (unary_expr
->u
.unary_expression
.type
) {
962 case UNARY_UNSIGNED_CONSTANT
:
963 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
965 case UNARY_SIGNED_CONSTANT
:
966 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
970 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
972 if (!strcmp(str
, "true") || !strcmp(str
, "TRUE")) {
974 } else if (!strcmp(str
, "false") || !strcmp(str
, "FALSE")) {
977 _FPERROR(efd
, "unexpected string \"%s\"", str
);
984 _FPERROR(efd
, "%s", "unexpected unary expression type");
994 enum bt_ctf_byte_order
byte_order_from_unary_expr(FILE *efd
,
995 struct ctf_node
*unary_expr
)
998 enum bt_ctf_byte_order bo
= BT_CTF_BYTE_ORDER_UNKNOWN
;
1000 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
1002 "\"byte_order\" attribute: expecting string");
1006 str
= unary_expr
->u
.unary_expression
.u
.string
;
1008 if (!strcmp(str
, "be") || !strcmp(str
, "network")) {
1009 bo
= BT_CTF_BYTE_ORDER_BIG_ENDIAN
;
1010 } else if (!strcmp(str
, "le")) {
1011 bo
= BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
;
1012 } else if (!strcmp(str
, "native")) {
1013 bo
= BT_CTF_BYTE_ORDER_NATIVE
;
1015 _FPERROR(efd
, "unexpected \"byte_order\" attribute value \"%s\"; should be \"be\", \"le\", \"network\", or \"native\"",
1025 enum bt_ctf_byte_order
get_real_byte_order(struct ctx
*ctx
,
1026 struct ctf_node
*uexpr
)
1028 enum bt_ctf_byte_order bo
= byte_order_from_unary_expr(ctx
->efd
, uexpr
);
1030 if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
1031 bo
= bt_ctf_trace_get_byte_order(ctx
->trace
);
1038 int is_align_valid(uint64_t align
)
1040 return (align
!= 0) && !(align
& (align
- 1));
1044 int get_type_specifier_name(struct ctx
*ctx
, struct ctf_node
*type_specifier
,
1049 if (type_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
1054 switch (type_specifier
->u
.type_specifier
.type
) {
1056 g_string_append(str
, "void");
1059 g_string_append(str
, "char");
1061 case TYPESPEC_SHORT
:
1062 g_string_append(str
, "short");
1065 g_string_append(str
, "int");
1068 g_string_append(str
, "long");
1070 case TYPESPEC_FLOAT
:
1071 g_string_append(str
, "float");
1073 case TYPESPEC_DOUBLE
:
1074 g_string_append(str
, "double");
1076 case TYPESPEC_SIGNED
:
1077 g_string_append(str
, "signed");
1079 case TYPESPEC_UNSIGNED
:
1080 g_string_append(str
, "unsigned");
1083 g_string_append(str
, "bool");
1085 case TYPESPEC_COMPLEX
:
1086 g_string_append(str
, "_Complex");
1088 case TYPESPEC_IMAGINARY
:
1089 g_string_append(str
, "_Imaginary");
1091 case TYPESPEC_CONST
:
1092 g_string_append(str
, "const");
1094 case TYPESPEC_ID_TYPE
:
1095 if (type_specifier
->u
.type_specifier
.id_type
) {
1096 g_string_append(str
,
1097 type_specifier
->u
.type_specifier
.id_type
);
1100 case TYPESPEC_STRUCT
:
1102 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1104 if (!node
->u
._struct
.name
) {
1105 _PERROR("%s", "unexpected empty structure name");
1110 g_string_append(str
, "struct ");
1111 g_string_append(str
, node
->u
._struct
.name
);
1114 case TYPESPEC_VARIANT
:
1116 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1118 if (!node
->u
.variant
.name
) {
1119 _PERROR("%s", "unexpected empty variant name");
1124 g_string_append(str
, "variant ");
1125 g_string_append(str
, node
->u
.variant
.name
);
1130 struct ctf_node
*node
= type_specifier
->u
.type_specifier
.node
;
1132 if (!node
->u
._enum
.enum_id
) {
1133 _PERROR("%s", "unexpected empty enum name");
1138 g_string_append(str
, "enum ");
1139 g_string_append(str
, node
->u
._enum
.enum_id
);
1142 case TYPESPEC_FLOATING_POINT
:
1143 case TYPESPEC_INTEGER
:
1144 case TYPESPEC_STRING
:
1146 _PERROR("%s", "unknown specifier");
1156 int get_type_specifier_list_name(struct ctx
*ctx
,
1157 struct ctf_node
*type_specifier_list
, GString
*str
)
1160 struct ctf_node
*iter
;
1161 int alias_item_nr
= 0;
1162 struct bt_list_head
*head
=
1163 &type_specifier_list
->u
.type_specifier_list
.head
;
1165 bt_list_for_each_entry(iter
, head
, siblings
) {
1166 if (alias_item_nr
!= 0) {
1167 g_string_append(str
, " ");
1171 ret
= get_type_specifier_name(ctx
, iter
, str
);
1182 GQuark
create_typealias_identifier(struct ctx
*ctx
,
1183 struct ctf_node
*type_specifier_list
,
1184 struct ctf_node
*node_type_declarator
)
1190 struct ctf_node
*iter
;
1191 struct bt_list_head
*pointers
=
1192 &node_type_declarator
->u
.type_declarator
.pointers
;
1194 str
= g_string_new("");
1195 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
1197 g_string_free(str
, TRUE
);
1201 bt_list_for_each_entry(iter
, pointers
, siblings
) {
1202 g_string_append(str
, " *");
1204 if (iter
->u
.pointer
.const_qualifier
) {
1205 g_string_append(str
, " const");
1209 str_c
= g_string_free(str
, FALSE
);
1210 qalias
= g_quark_from_string(str_c
);
1218 int visit_type_declarator(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1219 GQuark
*field_name
, struct ctf_node
*node_type_declarator
,
1220 struct bt_ctf_field_type
**field_decl
,
1221 struct bt_ctf_field_type
*nested_decl
)
1224 * During this whole function, nested_decl is always OURS,
1225 * whereas field_decl is an output which we create, but
1226 * belongs to the caller (it is moved).
1232 /* Validate type declarator node */
1233 if (node_type_declarator
) {
1234 if (node_type_declarator
->u
.type_declarator
.type
==
1240 /* TODO: GCC bitfields not supported yet */
1241 if (node_type_declarator
->u
.type_declarator
.bitfield_len
!=
1243 _PERROR("%s", "GCC bitfields are not supported as of this version");
1249 /* Find the right nested declaration if not provided */
1251 struct bt_list_head
*pointers
=
1252 &node_type_declarator
->u
.type_declarator
.pointers
;
1254 if (node_type_declarator
&& !bt_list_empty(pointers
)) {
1256 _BT_CTF_FIELD_TYPE_INIT(nested_decl_copy
);
1259 * If we have a pointer declarator, it HAS to
1260 * be present in the typealiases (else fail).
1262 qalias
= create_typealias_identifier(ctx
,
1263 type_specifier_list
, node_type_declarator
);
1265 ctx_decl_scope_lookup_alias(ctx
->current_scope
,
1266 g_quark_to_string(qalias
), -1);
1268 _PERROR("cannot find typealias \"%s\"",
1269 g_quark_to_string(qalias
));
1274 /* Make a copy of it */
1275 nested_decl_copy
= bt_ctf_field_type_copy(nested_decl
);
1276 BT_PUT(nested_decl
);
1277 if (!nested_decl_copy
) {
1278 _PERROR("%s", "cannot copy nested declaration");
1283 BT_MOVE(nested_decl
, nested_decl_copy
);
1285 /* Force integer's base to 16 since it's a pointer */
1286 if (bt_ctf_field_type_is_integer(nested_decl
)) {
1287 bt_ctf_field_type_integer_set_base(nested_decl
,
1288 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
1291 ret
= visit_type_specifier_list(ctx
,
1292 type_specifier_list
, &nested_decl
);
1294 assert(!nested_decl
);
1300 assert(nested_decl
);
1302 if (!node_type_declarator
) {
1303 BT_MOVE(*field_decl
, nested_decl
);
1307 if (node_type_declarator
->u
.type_declarator
.type
== TYPEDEC_ID
) {
1308 if (node_type_declarator
->u
.type_declarator
.u
.id
) {
1310 node_type_declarator
->u
.type_declarator
.u
.id
;
1312 *field_name
= g_quark_from_string(id
);
1317 BT_MOVE(*field_decl
, nested_decl
);
1320 struct ctf_node
*first
;
1321 _BT_CTF_FIELD_TYPE_INIT(decl
);
1322 _BT_CTF_FIELD_TYPE_INIT(outer_field_decl
);
1323 struct bt_list_head
*length
=
1324 &node_type_declarator
->
1325 u
.type_declarator
.u
.nested
.length
;
1327 /* Create array/sequence, pass nested_decl as child */
1328 if (bt_list_empty(length
)) {
1330 "expecting length field reference or value");
1335 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1336 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1341 switch (first
->u
.unary_expression
.type
) {
1342 case UNARY_UNSIGNED_CONSTANT
:
1345 _BT_CTF_FIELD_TYPE_INIT(array_decl
);
1347 len
= first
->u
.unary_expression
.u
.unsigned_constant
;
1348 array_decl
= bt_ctf_field_type_array_create(nested_decl
,
1350 BT_PUT(nested_decl
);
1353 "cannot create array declaration");
1358 BT_MOVE(decl
, array_decl
);
1363 /* Lookup unsigned integer definition, create seq. */
1364 _BT_CTF_FIELD_TYPE_INIT(seq_decl
);
1365 char *length_name
= concatenate_unary_strings(length
);
1372 seq_decl
= bt_ctf_field_type_sequence_create(
1373 nested_decl
, length_name
);
1374 g_free(length_name
);
1375 BT_PUT(nested_decl
);
1378 "cannot create sequence declaration");
1383 BT_MOVE(decl
, seq_decl
);
1391 assert(!nested_decl
);
1393 assert(!*field_decl
);
1396 * At this point, we found the next nested declaration.
1397 * We currently own this (and lost the ownership of
1398 * nested_decl in the meantime). Pass this next
1399 * nested declaration as the content of the outer
1400 * container, MOVING its ownership.
1402 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1404 node_type_declarator
->
1405 u
.type_declarator
.u
.nested
.type_declarator
,
1406 &outer_field_decl
, decl
);
1409 assert(!outer_field_decl
);
1414 assert(outer_field_decl
);
1415 BT_MOVE(*field_decl
, outer_field_decl
);
1419 BT_PUT(nested_decl
);
1420 assert(*field_decl
);
1425 BT_PUT(nested_decl
);
1426 BT_PUT(*field_decl
);
1432 int visit_struct_decl_field(struct ctx
*ctx
,
1433 struct bt_ctf_field_type
*struct_decl
,
1434 struct ctf_node
*type_specifier_list
,
1435 struct bt_list_head
*type_declarators
)
1438 struct ctf_node
*iter
;
1439 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1441 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1444 const char *field_name
;
1445 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1447 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1448 &qfield_name
, iter
, &field_decl
, NULL
);
1450 assert(!field_decl
);
1451 _PERROR("%s", "unable to find structure field declaration type");
1456 field_name
= g_quark_to_string(qfield_name
);
1458 /* Check if field with same name already exists */
1460 bt_ctf_field_type_structure_get_field_type_by_name(
1461 struct_decl
, field_name
);
1463 BT_PUT(efield_decl
);
1464 _PERROR("duplicate field \"%s\" in structure",
1470 /* Add field to structure */
1471 ret
= bt_ctf_field_type_structure_add_field(struct_decl
,
1472 field_decl
, field_name
);
1475 _PERROR("cannot add field \"%s\" to structure",
1476 g_quark_to_string(qfield_name
));
1490 int visit_variant_decl_field(struct ctx
*ctx
,
1491 struct bt_ctf_field_type
*variant_decl
,
1492 struct ctf_node
*type_specifier_list
,
1493 struct bt_list_head
*type_declarators
)
1496 struct ctf_node
*iter
;
1497 _BT_CTF_FIELD_TYPE_INIT(field_decl
);
1499 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1502 const char *field_name
;
1503 _BT_CTF_FIELD_TYPE_INIT(efield_decl
);
1505 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1506 &qfield_name
, iter
, &field_decl
, NULL
);
1508 assert(!field_decl
);
1510 "unable to find variant field declaration type");
1515 field_name
= g_quark_to_string(qfield_name
);
1517 /* Check if field with same name already exists */
1519 bt_ctf_field_type_variant_get_field_type_by_name(
1520 variant_decl
, field_name
);
1522 BT_PUT(efield_decl
);
1523 _PERROR("duplicate field \"%s\" in variant",
1529 /* Add field to structure */
1530 ret
= bt_ctf_field_type_variant_add_field(variant_decl
,
1531 field_decl
, field_name
);
1534 _PERROR("cannot add field \"%s\" to variant",
1535 g_quark_to_string(qfield_name
));
1549 int visit_typedef(struct ctx
*ctx
, struct ctf_node
*type_specifier_list
,
1550 struct bt_list_head
*type_declarators
)
1554 struct ctf_node
*iter
;
1555 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1557 bt_list_for_each_entry(iter
, type_declarators
, siblings
) {
1558 ret
= visit_type_declarator(ctx
, type_specifier_list
,
1559 &qidentifier
, iter
, &type_decl
, NULL
);
1561 _PERROR("%s", "problem creating type declaration");
1566 /* Do not allow typedef and typealias of untagged variants */
1567 if (bt_ctf_field_type_is_variant(type_decl
)) {
1568 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1569 _PERROR("%s", "typedef of untagged variant is not allowed");
1575 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1576 g_quark_to_string(qidentifier
), type_decl
);
1578 _PERROR("cannot register typedef \"%s\"",
1579 g_quark_to_string(qidentifier
));
1591 int visit_typealias(struct ctx
*ctx
, struct ctf_node
*target
,
1592 struct ctf_node
*alias
)
1596 struct ctf_node
*node
;
1597 GQuark qdummy_field_name
;
1598 _BT_CTF_FIELD_TYPE_INIT(type_decl
);
1600 /* Create target type declaration */
1601 if (bt_list_empty(&target
->u
.typealias_target
.type_declarators
)) {
1604 node
= _BT_LIST_FIRST_ENTRY(
1605 &target
->u
.typealias_target
.type_declarators
,
1606 struct ctf_node
, siblings
);
1609 ret
= visit_type_declarator(ctx
,
1610 target
->u
.typealias_target
.type_specifier_list
,
1611 &qdummy_field_name
, node
, &type_decl
, NULL
);
1614 _PERROR("%s", "problem creating type declaration");
1618 /* Do not allow typedef and typealias of untagged variants */
1619 if (bt_ctf_field_type_is_variant(type_decl
)) {
1620 if (bt_ctf_field_type_variant_get_tag_name(type_decl
)) {
1622 "typealias of untagged variant is not allowed");
1629 * The semantic validator does not check whether the target is
1630 * abstract or not (if it has an identifier). Check it here.
1632 if (qdummy_field_name
!= 0) {
1633 _PERROR("%s", "expecting empty identifier");
1638 /* Create alias identifier */
1639 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.typealias_alias
.type_declarators
,
1640 struct ctf_node
, siblings
);
1641 qalias
= create_typealias_identifier(ctx
,
1642 alias
->u
.typealias_alias
.type_specifier_list
, node
);
1643 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
,
1644 g_quark_to_string(qalias
), type_decl
);
1646 _PERROR("cannot register typealias \"%s\"",
1647 g_quark_to_string(qalias
));
1658 int visit_struct_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1659 struct bt_ctf_field_type
*struct_decl
)
1663 switch (entry_node
->type
) {
1665 ret
= visit_typedef(ctx
,
1666 entry_node
->u
._typedef
.type_specifier_list
,
1667 &entry_node
->u
._typedef
.type_declarators
);
1670 "cannot add typedef in \"struct\" declaration");
1674 case NODE_TYPEALIAS
:
1675 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1676 entry_node
->u
.typealias
.alias
);
1679 "cannot add typealias in \"struct\" declaration");
1683 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1685 ret
= visit_struct_decl_field(ctx
, struct_decl
,
1686 entry_node
->u
.struct_or_variant_declaration
.
1687 type_specifier_list
,
1688 &entry_node
->u
.struct_or_variant_declaration
.
1695 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1705 int visit_variant_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
1706 struct bt_ctf_field_type
*variant_decl
)
1710 switch (entry_node
->type
) {
1712 ret
= visit_typedef(ctx
,
1713 entry_node
->u
._typedef
.type_specifier_list
,
1714 &entry_node
->u
._typedef
.type_declarators
);
1717 "cannot add typedef in \"variant\" declaration");
1721 case NODE_TYPEALIAS
:
1722 ret
= visit_typealias(ctx
, entry_node
->u
.typealias
.target
,
1723 entry_node
->u
.typealias
.alias
);
1726 "cannot add typealias in \"variant\" declaration");
1730 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1732 ret
= visit_variant_decl_field(ctx
, variant_decl
,
1733 entry_node
->u
.struct_or_variant_declaration
.
1734 type_specifier_list
,
1735 &entry_node
->u
.struct_or_variant_declaration
.
1742 _PERROR("unexpected node type: %d", (int) entry_node
->type
);
1752 int visit_struct_decl(struct ctx
*ctx
, const char *name
,
1753 struct bt_list_head
*decl_list
, int has_body
,
1754 struct bt_list_head
*min_align
,
1755 struct bt_ctf_field_type
**struct_decl
)
1759 *struct_decl
= NULL
;
1761 /* For named struct (without body), lookup in declaration scope */
1763 _BT_CTF_FIELD_TYPE_INIT(struct_decl_copy
);
1770 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1772 if (!*struct_decl
) {
1773 _PERROR("cannot find \"struct %s\"", name
);
1778 /* Make a copy of it */
1779 struct_decl_copy
= bt_ctf_field_type_copy(*struct_decl
);
1780 if (!struct_decl_copy
) {
1782 "cannot create copy of structure declaration");
1787 BT_MOVE(*struct_decl
, struct_decl_copy
);
1789 struct ctf_node
*entry_node
;
1790 uint64_t min_align_value
= 0;
1793 _BT_CTF_FIELD_TYPE_INIT(estruct_decl
);
1795 estruct_decl
= ctx_decl_scope_lookup_struct(
1796 ctx
->current_scope
, name
, 1);
1798 BT_PUT(estruct_decl
);
1799 _PERROR("\"struct %s\" already declared in local scope",
1806 if (!bt_list_empty(min_align
)) {
1807 ret
= get_unary_unsigned(min_align
, &min_align_value
);
1809 _PERROR("%s", "unexpected unary expression for structure declaration's \"align\" attribute");
1814 *struct_decl
= bt_ctf_field_type_structure_create();
1815 if (!*struct_decl
) {
1816 _PERROR("%s", "cannot create structure declaration");
1821 ret
= ctx_push_scope(ctx
);
1823 _PERROR("%s", "cannot push scope");
1827 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1828 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1839 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1840 name
, *struct_decl
);
1842 _PERROR("cannot register \"struct %s\" in declaration scope",
1852 BT_PUT(*struct_decl
);
1858 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1859 const char *tag
, struct bt_list_head
*decl_list
,
1860 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1863 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1865 *variant_decl
= NULL
;
1867 /* For named variant (without body), lookup in declaration scope */
1869 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1876 untagged_variant_decl
=
1877 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1879 if (!untagged_variant_decl
) {
1880 _PERROR("cannot find \"variant %s\"", name
);
1885 /* Make a copy of it */
1886 variant_decl_copy
= bt_ctf_field_type_copy(
1887 untagged_variant_decl
);
1888 if (!variant_decl_copy
) {
1890 "cannot create copy of structure declaration");
1895 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1897 struct ctf_node
*entry_node
;
1900 struct bt_ctf_field_type
*evariant_decl
=
1901 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1904 if (evariant_decl
) {
1905 BT_PUT(evariant_decl
);
1906 _PERROR("\"variant %s\" already declared in local scope",
1913 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1915 if (!untagged_variant_decl
) {
1916 _PERROR("%s", "cannot create variant declaration");
1921 ret
= ctx_push_scope(ctx
);
1923 _PERROR("%s", "cannot push scope");
1927 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1928 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1929 untagged_variant_decl
);
1939 ret
= ctx_decl_scope_register_variant(
1940 ctx
->current_scope
, name
,
1941 untagged_variant_decl
);
1943 _PERROR("cannot register \"variant %s\" in declaration scope",
1951 * If tagged, create tagged variant and return; otherwise
1952 * return untagged variant.
1955 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1958 * At this point, we have a fresh untagged variant; nobody
1959 * else owns it. Set its tag now.
1961 ret
= bt_ctf_field_type_variant_set_tag_name(
1962 untagged_variant_decl
, tag
);
1967 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1970 assert(!untagged_variant_decl
);
1971 assert(*variant_decl
);
1976 BT_PUT(untagged_variant_decl
);
1977 BT_PUT(*variant_decl
);
1983 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
1984 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
1988 struct ctf_node
*iter
;
1989 int64_t start
= 0, end
= 0;
1990 const char *label
= enumerator
->u
.enumerator
.id
;
1991 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1993 bt_list_for_each_entry(iter
, values
, siblings
) {
1996 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
1997 _PERROR("wrong unary expression for enumeration label \"%s\"",
2009 switch (iter
->u
.unary_expression
.type
) {
2010 case UNARY_SIGNED_CONSTANT
:
2011 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2013 case UNARY_UNSIGNED_CONSTANT
:
2015 iter
->u
.unary_expression
.u
.unsigned_constant
;
2018 _PERROR("invalid enumeration entry: \"%s\"",
2025 _PERROR("invalid enumeration entry: \"%s\"",
2045 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2048 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2049 label
, (uint64_t) start
, (uint64_t) end
);
2052 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2064 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2065 struct ctf_node
*container_type
,
2066 struct bt_list_head
*enumerator_list
,
2068 struct bt_ctf_field_type
**enum_decl
)
2072 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2076 /* For named enum (without body), lookup in declaration scope */
2078 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2085 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2088 _PERROR("cannot find \"enum %s\"", name
);
2093 /* Make a copy of it */
2094 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2095 if (!enum_decl_copy
) {
2097 "cannot create copy of enumeration declaration");
2103 BT_MOVE(*enum_decl
, enum_decl_copy
);
2105 struct ctf_node
*iter
;
2106 int64_t last_value
= 0;
2109 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2111 eenum_decl
= ctx_decl_scope_lookup_enum(
2112 ctx
->current_scope
, name
, 1);
2115 _PERROR("\"enum %s\" already declared in local scope",
2122 if (!container_type
) {
2123 integer_decl
= ctx_decl_scope_lookup_alias(
2124 ctx
->current_scope
, "int", -1);
2125 if (!integer_decl
) {
2126 _PERROR("%s", "cannot find \"int\" type for enumeration");
2131 ret
= visit_type_declarator(ctx
, container_type
,
2132 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2134 assert(!integer_decl
);
2140 assert(integer_decl
);
2142 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2143 _PERROR("%s", "container type for enumeration is not an integer");
2148 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2150 _PERROR("%s", "cannot create enumeration declaration");
2155 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2156 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2158 bt_ctf_field_type_integer_get_signed(integer_decl
));
2165 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2173 BT_PUT(integer_decl
);
2178 BT_PUT(integer_decl
);
2185 int visit_type_specifier(struct ctx
*ctx
,
2186 struct ctf_node
*type_specifier_list
,
2187 struct bt_ctf_field_type
**decl
)
2190 GString
*str
= NULL
;
2191 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2194 str
= g_string_new("");
2195 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2200 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2202 _PERROR("cannot find type alias \"%s\"", str
->str
);
2207 /* Make a copy of the type declaration */
2208 decl_copy
= bt_ctf_field_type_copy(*decl
);
2210 _PERROR("%s", "cannot create copy of type declaration");
2215 BT_MOVE(*decl
, decl_copy
);
2216 (void) g_string_free(str
, TRUE
);
2223 (void) g_string_free(str
, TRUE
);
2232 int visit_integer_decl(struct ctx
*ctx
,
2233 struct bt_list_head
*expressions
,
2234 struct bt_ctf_field_type
**integer_decl
)
2239 struct ctf_node
*expression
;
2240 uint64_t alignment
= 0, size
= 0;
2241 struct bt_ctf_clock
*mapped_clock
= NULL
;
2242 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2243 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2244 enum bt_ctf_byte_order byte_order
=
2245 bt_ctf_trace_get_byte_order(ctx
->trace
);
2247 *integer_decl
= NULL
;
2249 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2250 struct ctf_node
*left
, *right
;
2252 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2253 struct ctf_node
, siblings
);
2254 right
= _BT_LIST_FIRST_ENTRY(
2255 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2258 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2263 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2264 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2265 _PERROR_DUP_ATTR("signed",
2266 "integer declaration");
2271 signedness
= get_boolean(ctx
->efd
, right
);
2272 if (signedness
< 0) {
2277 _SET(&set
, _INTEGER_SIGNED_SET
);
2278 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2280 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2281 _PERROR_DUP_ATTR("byte_order",
2282 "integer declaration");
2287 byte_order
= get_real_byte_order(ctx
, right
);
2288 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2289 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2294 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2295 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2296 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2297 _PERROR_DUP_ATTR("size",
2298 "integer declaration");
2303 if (right
->u
.unary_expression
.type
!=
2304 UNARY_UNSIGNED_CONSTANT
) {
2305 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2310 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2312 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2315 } else if (size
> 64) {
2316 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2321 _SET(&set
, _INTEGER_SIZE_SET
);
2322 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2324 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2325 _PERROR_DUP_ATTR("align",
2326 "integer declaration");
2331 if (right
->u
.unary_expression
.type
!=
2332 UNARY_UNSIGNED_CONSTANT
) {
2333 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2339 right
->u
.unary_expression
.u
.unsigned_constant
;
2340 if (!is_align_valid(alignment
)) {
2341 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2346 _SET(&set
, _INTEGER_ALIGN_SET
);
2347 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2348 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2349 _PERROR_DUP_ATTR("base", "integer declaration");
2354 switch (right
->u
.unary_expression
.type
) {
2355 case UNARY_UNSIGNED_CONSTANT
:
2357 uint64_t constant
= right
->u
.unary_expression
.
2358 u
.unsigned_constant
;
2362 base
= BT_CTF_INTEGER_BASE_BINARY
;
2365 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2368 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2371 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2374 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2375 right
->u
.unary_expression
.u
.unsigned_constant
);
2383 char *s_right
= concatenate_unary_strings(
2384 &expression
->u
.ctf_expression
.right
);
2386 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2391 if (!strcmp(s_right
, "decimal") ||
2392 !strcmp(s_right
, "dec") ||
2393 !strcmp(s_right
, "d") ||
2394 !strcmp(s_right
, "i") ||
2395 !strcmp(s_right
, "u")) {
2396 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2397 } else if (!strcmp(s_right
, "hexadecimal") ||
2398 !strcmp(s_right
, "hex") ||
2399 !strcmp(s_right
, "x") ||
2400 !strcmp(s_right
, "X") ||
2401 !strcmp(s_right
, "p")) {
2402 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2403 } else if (!strcmp(s_right
, "octal") ||
2404 !strcmp(s_right
, "oct") ||
2405 !strcmp(s_right
, "o")) {
2406 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2407 } else if (!strcmp(s_right
, "binary") ||
2408 !strcmp(s_right
, "b")) {
2409 base
= BT_CTF_INTEGER_BASE_BINARY
;
2411 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2422 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2427 _SET(&set
, _INTEGER_BASE_SET
);
2428 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2432 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2433 _PERROR_DUP_ATTR("encoding",
2434 "integer declaration");
2439 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2440 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2445 s_right
= concatenate_unary_strings(
2446 &expression
->u
.ctf_expression
.right
);
2448 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2453 if (!strcmp(s_right
, "UTF8") ||
2454 !strcmp(s_right
, "utf8") ||
2455 !strcmp(s_right
, "utf-8") ||
2456 !strcmp(s_right
, "UTF-8")) {
2457 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2458 } else if (!strcmp(s_right
, "ASCII") ||
2459 !strcmp(s_right
, "ascii")) {
2460 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2461 } else if (!strcmp(s_right
, "none")) {
2462 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2464 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2472 _SET(&set
, _INTEGER_ENCODING_SET
);
2473 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2474 const char *clock_name
;
2476 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2477 _PERROR_DUP_ATTR("map", "integer declaration");
2482 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2483 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2489 get_map_clock_name_value(
2490 &expression
->u
.ctf_expression
.right
);
2492 char *s_right
= concatenate_unary_strings(
2493 &expression
->u
.ctf_expression
.right
);
2496 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2501 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock: \"%s\"",
2503 _SET(&set
, _INTEGER_MAP_SET
);
2508 mapped_clock
= bt_ctf_trace_get_clock_by_name(
2509 ctx
->trace
, clock_name
);
2510 if (!mapped_clock
) {
2511 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock \"%s\"",
2517 _SET(&set
, _INTEGER_MAP_SET
);
2519 _PWARNING("unknown attribute \"%s\" in integer declaration",
2520 left
->u
.unary_expression
.u
.string
);
2524 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2526 "missing \"size\" attribute in integer declaration");
2531 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2532 if (size
% CHAR_BIT
) {
2533 /* Bit-packed alignment */
2536 /* Byte-packed alignment */
2537 alignment
= CHAR_BIT
;
2541 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2542 if (!*integer_decl
) {
2543 _PERROR("%s", "cannot create integer declaration");
2548 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2549 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2550 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2551 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2552 (unsigned int) alignment
);
2553 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2557 ret
|= bt_ctf_field_type_integer_set_mapped_clock(
2558 *integer_decl
, mapped_clock
);
2559 bt_put(mapped_clock
);
2560 mapped_clock
= NULL
;
2564 _PERROR("%s", "cannot configure integer declaration");
2573 bt_put(mapped_clock
);
2576 BT_PUT(*integer_decl
);
2582 int visit_floating_point_number_decl(struct ctx
*ctx
,
2583 struct bt_list_head
*expressions
,
2584 struct bt_ctf_field_type
**float_decl
)
2588 struct ctf_node
*expression
;
2589 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2590 enum bt_ctf_byte_order byte_order
=
2591 bt_ctf_trace_get_byte_order(ctx
->trace
);
2595 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2596 struct ctf_node
*left
, *right
;
2598 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2599 struct ctf_node
, siblings
);
2600 right
= _BT_LIST_FIRST_ENTRY(
2601 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2604 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2609 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2610 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2611 _PERROR_DUP_ATTR("byte_order",
2612 "floating point number declaration");
2617 byte_order
= get_real_byte_order(ctx
, right
);
2618 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2619 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2624 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2625 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2627 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2628 _PERROR_DUP_ATTR("exp_dig",
2629 "floating point number declaration");
2634 if (right
->u
.unary_expression
.type
!=
2635 UNARY_UNSIGNED_CONSTANT
) {
2636 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2641 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2642 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2643 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2645 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2646 _PERROR_DUP_ATTR("mant_dig",
2647 "floating point number declaration");
2652 if (right
->u
.unary_expression
.type
!=
2653 UNARY_UNSIGNED_CONSTANT
) {
2654 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2659 mant_dig
= right
->u
.unary_expression
.u
.
2661 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2662 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2664 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2665 _PERROR_DUP_ATTR("align",
2666 "floating point number declaration");
2671 if (right
->u
.unary_expression
.type
!=
2672 UNARY_UNSIGNED_CONSTANT
) {
2673 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2678 alignment
= right
->u
.unary_expression
.u
.
2681 if (!is_align_valid(alignment
)) {
2682 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2687 _SET(&set
, _FLOAT_ALIGN_SET
);
2689 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2690 left
->u
.unary_expression
.u
.string
);
2694 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2695 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2700 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2701 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2706 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2707 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2708 /* Bit-packed alignment */
2711 /* Byte-packed alignment */
2712 alignment
= CHAR_BIT
;
2716 *float_decl
= bt_ctf_field_type_floating_point_create();
2719 "cannot create floating point number declaration");
2724 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2725 *float_decl
, exp_dig
);
2726 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2727 *float_decl
, mant_dig
);
2728 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2729 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2732 "cannot configure floating point number declaration");
2740 BT_PUT(*float_decl
);
2746 int visit_string_decl(struct ctx
*ctx
,
2747 struct bt_list_head
*expressions
,
2748 struct bt_ctf_field_type
**string_decl
)
2752 struct ctf_node
*expression
;
2753 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2755 *string_decl
= NULL
;
2757 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2758 struct ctf_node
*left
, *right
;
2760 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2761 struct ctf_node
, siblings
);
2762 right
= _BT_LIST_FIRST_ENTRY(
2763 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2766 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2771 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2774 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2775 _PERROR_DUP_ATTR("encoding",
2776 "string declaration");
2781 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2782 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2787 s_right
= concatenate_unary_strings(
2788 &expression
->u
.ctf_expression
.right
);
2790 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2795 if (!strcmp(s_right
, "UTF8") ||
2796 !strcmp(s_right
, "utf8") ||
2797 !strcmp(s_right
, "utf-8") ||
2798 !strcmp(s_right
, "UTF-8")) {
2799 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2800 } else if (!strcmp(s_right
, "ASCII") ||
2801 !strcmp(s_right
, "ascii")) {
2802 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2803 } else if (!strcmp(s_right
, "none")) {
2804 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2806 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2814 _SET(&set
, _STRING_ENCODING_SET
);
2816 _PWARNING("unknown attribute \"%s\" in string declaration",
2817 left
->u
.unary_expression
.u
.string
);
2821 *string_decl
= bt_ctf_field_type_string_create();
2822 if (!*string_decl
) {
2823 _PERROR("%s", "cannot create string declaration");
2828 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2830 _PERROR("%s", "cannot configure string declaration");
2838 BT_PUT(*string_decl
);
2844 int visit_type_specifier_list(struct ctx
*ctx
,
2845 struct ctf_node
*ts_list
,
2846 struct bt_ctf_field_type
**decl
)
2849 struct ctf_node
*first
, *node
;
2853 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2858 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2859 struct ctf_node
, siblings
);
2860 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2865 node
= first
->u
.type_specifier
.node
;
2867 switch (first
->u
.type_specifier
.type
) {
2868 case TYPESPEC_INTEGER
:
2869 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2876 case TYPESPEC_FLOATING_POINT
:
2877 ret
= visit_floating_point_number_decl(ctx
,
2878 &node
->u
.floating_point
.expressions
, decl
);
2884 case TYPESPEC_STRING
:
2885 ret
= visit_string_decl(ctx
,
2886 &node
->u
.string
.expressions
, decl
);
2892 case TYPESPEC_STRUCT
:
2893 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2894 &node
->u
._struct
.declaration_list
,
2895 node
->u
._struct
.has_body
,
2896 &node
->u
._struct
.min_align
, decl
);
2902 case TYPESPEC_VARIANT
:
2903 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2904 node
->u
.variant
.choice
,
2905 &node
->u
.variant
.declaration_list
,
2906 node
->u
.variant
.has_body
, decl
);
2913 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2914 node
->u
._enum
.container_type
,
2915 &node
->u
._enum
.enumerator_list
,
2916 node
->u
._enum
.has_body
, decl
);
2924 case TYPESPEC_SHORT
:
2927 case TYPESPEC_FLOAT
:
2928 case TYPESPEC_DOUBLE
:
2929 case TYPESPEC_SIGNED
:
2930 case TYPESPEC_UNSIGNED
:
2932 case TYPESPEC_COMPLEX
:
2933 case TYPESPEC_IMAGINARY
:
2934 case TYPESPEC_CONST
:
2935 case TYPESPEC_ID_TYPE
:
2936 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2943 _PERROR("unexpected node type: %d",
2944 (int) first
->u
.type_specifier
.type
);
2960 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2961 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2966 _BT_CTF_FIELD_TYPE_INIT(decl
);
2968 switch (node
->type
) {
2970 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2971 &node
->u
._typedef
.type_declarators
);
2974 "cannot add typedef in \"event\" declaration");
2978 case NODE_TYPEALIAS
:
2979 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
2980 node
->u
.typealias
.alias
);
2982 _PERROR("%s", "cannot add typealias in \"event\" declaration");
2986 case NODE_CTF_EXPRESSION
:
2988 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2994 if (!strcmp(left
, "name")) {
2995 /* This is already known at this stage */
2996 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
2997 _PERROR_DUP_ATTR("name", "event declaration");
3002 _SET(set
, _EVENT_NAME_SET
);
3003 } else if (!strcmp(left
, "id")) {
3006 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3007 _PERROR_DUP_ATTR("id", "event declaration");
3012 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3014 if (ret
|| id
< 0) {
3015 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3020 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3023 "cannot set event declaration's ID");
3027 _SET(set
, _EVENT_ID_SET
);
3028 } else if (!strcmp(left
, "stream_id")) {
3029 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3030 _PERROR_DUP_ATTR("stream_id",
3031 "event declaration");
3036 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3037 (uint64_t *) stream_id
);
3038 if (ret
|| *stream_id
< 0) {
3039 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3044 _SET(set
, _EVENT_STREAM_ID_SET
);
3045 } else if (!strcmp(left
, "context")) {
3046 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3047 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3052 ret
= visit_type_specifier_list(ctx
,
3053 _BT_LIST_FIRST_ENTRY(
3054 &node
->u
.ctf_expression
.right
,
3055 struct ctf_node
, siblings
),
3058 _PERROR("%s", "cannot create event context declaration");
3063 ret
= bt_ctf_event_class_set_context_type(
3067 _PERROR("%s", "cannot set event's context declaration");
3071 _SET(set
, _EVENT_CONTEXT_SET
);
3072 } else if (!strcmp(left
, "fields")) {
3073 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3074 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3079 ret
= visit_type_specifier_list(ctx
,
3080 _BT_LIST_FIRST_ENTRY(
3081 &node
->u
.ctf_expression
.right
,
3082 struct ctf_node
, siblings
),
3085 _PERROR("%s", "cannot create event payload declaration");
3090 ret
= bt_ctf_event_class_set_payload_type(
3094 _PERROR("%s", "cannot set event's payload declaration");
3098 _SET(set
, _EVENT_FIELDS_SET
);
3099 } else if (!strcmp(left
, "loglevel")) {
3100 uint64_t loglevel_value
;
3101 const char *loglevel_str
;
3102 struct bt_value
*value_obj
, *str_obj
;
3104 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3105 _PERROR_DUP_ATTR("loglevel",
3106 "event declaration");
3111 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3114 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3118 value_obj
= bt_value_integer_create_init(loglevel_value
);
3120 _PERROR("%s", "cannot allocate memory for loglevel value object");
3124 if (bt_ctf_event_class_set_attribute(event_class
,
3125 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3126 _PERROR("%s", "cannot set loglevel value");
3131 loglevel_str
= print_loglevel(loglevel_value
);
3133 str_obj
= bt_value_string_create_init(loglevel_str
);
3134 if (bt_ctf_event_class_set_attribute(event_class
,
3135 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3136 _PERROR("%s", "cannot set loglevel string");
3144 _SET(set
, _EVENT_LOGLEVEL_SET
);
3145 } else if (!strcmp(left
, "model.emf.uri")) {
3148 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3149 _PERROR_DUP_ATTR("model.emf.uri",
3150 "event declaration");
3155 right
= concatenate_unary_strings(
3156 &node
->u
.ctf_expression
.right
);
3158 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3163 // TODO: FIXME: set model EMF URI here
3166 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3168 _PWARNING("unknown attribute \"%s\" in event declaration",
3194 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3198 struct ctf_node
*iter
;
3199 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3201 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3202 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3206 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3211 if (!strcmp(left
, "name")) {
3212 name
= concatenate_unary_strings(
3213 &iter
->u
.ctf_expression
.right
);
3215 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3237 int reset_event_decl_types(struct ctx
*ctx
,
3238 struct bt_ctf_event_class
*event_class
)
3243 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3245 _PERROR("%s", "cannot set initial NULL event context");
3249 /* Event payload. */
3250 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3252 _PERROR("%s", "cannot set initial NULL event payload");
3260 int reset_stream_decl_types(struct ctx
*ctx
,
3261 struct bt_ctf_stream_class
*stream_class
)
3265 /* Packet context. */
3266 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3268 _PERROR("%s", "cannot set initial empty packet context");
3273 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3275 _PERROR("%s", "cannot set initial empty event header");
3279 /* Event context. */
3280 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3282 _PERROR("%s", "cannot set initial empty stream event context");
3290 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3293 struct bt_ctf_stream_class
*stream_class
;
3295 stream_class
= bt_ctf_stream_class_create(NULL
);
3296 if (!stream_class
) {
3297 _PERROR("%s", "cannot create stream class");
3302 * Set packet context, event header, and event context to NULL to
3303 * override the default ones.
3305 ret
= reset_stream_decl_types(ctx
, stream_class
);
3310 return stream_class
;
3313 BT_PUT(stream_class
);
3319 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3324 struct ctf_node
*iter
;
3325 int64_t stream_id
= -1;
3326 char *event_name
= NULL
;
3327 struct bt_ctf_event_class
*event_class
= NULL
;
3328 struct bt_ctf_event_class
*eevent_class
;
3329 struct bt_ctf_stream_class
*stream_class
;
3330 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3332 if (node
->visited
) {
3336 node
->visited
= TRUE
;
3337 event_name
= get_event_decl_name(ctx
, node
);
3340 "missing \"name\" attribute in event declaration");
3345 event_class
= bt_ctf_event_class_create(event_name
);
3348 * Unset context and fields to override the default ones.
3350 ret
= reset_event_decl_types(ctx
, event_class
);
3356 ret
= ctx_push_scope(ctx
);
3358 _PERROR("%s", "cannot push scope");
3362 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3363 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3370 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3372 struct bt_ctf_stream_class
*new_stream_class
;
3374 /* Allow missing stream_id if there is only a single stream */
3375 switch (g_hash_table_size(ctx
->stream_classes
)) {
3377 /* Create stream if there's none */
3378 new_stream_class
= create_reset_stream_class(ctx
);
3379 if (!new_stream_class
) {
3384 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3386 _PERROR("%s", "cannot set stream's ID");
3387 BT_PUT(new_stream_class
);
3393 /* Move reference to visitor's context */
3394 g_hash_table_insert(ctx
->stream_classes
,
3395 (gpointer
) stream_id
, new_stream_class
);
3396 new_stream_class
= NULL
;
3400 /* Single stream: get its ID */
3401 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3402 stream_id
= (int64_t) keys
->data
;
3407 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3415 assert(stream_id
>= 0);
3417 /* We have the stream ID now; borrow the stream class if found */
3418 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3419 (gpointer
) stream_id
);
3420 if (!stream_class
) {
3421 _PERROR("cannot find stream class with ID %" PRId64
,
3427 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3428 /* Allow only one event without ID per stream */
3429 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3432 "missing \"id\" field in event declaration");
3438 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3440 _PERROR("%s", "cannot set event's ID");
3445 event_id
= bt_ctf_event_class_get_id(event_class
);
3447 _PERROR("%s", "cannot get event's ID");
3452 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3455 BT_PUT(eevent_class
);
3456 _PERROR("%s", "duplicate event with ID %" PRId64
" in same stream");
3461 eevent_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
,
3464 BT_PUT(eevent_class
);
3465 eevent_class
= NULL
;
3467 "duplicate event with name \"%s\" in same stream");
3473 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3474 BT_PUT(event_class
);
3478 _PERROR("%s", "cannot add event class to stream class");
3487 BT_PUT(event_class
);
3489 /* stream_class is borrowed; it still belongs to the hash table */
3495 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3496 struct bt_ctf_stream_class
*stream_class
, int *set
)
3500 _BT_CTF_FIELD_TYPE_INIT(decl
);
3502 switch (node
->type
) {
3504 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3505 &node
->u
._typedef
.type_declarators
);
3508 "cannot add typedef in \"stream\" declaration");
3512 case NODE_TYPEALIAS
:
3513 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3514 node
->u
.typealias
.alias
);
3516 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3520 case NODE_CTF_EXPRESSION
:
3522 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3528 if (!strcmp(left
, "id")) {
3532 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3533 _PERROR_DUP_ATTR("id", "stream declaration");
3538 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3540 if (ret
|| id
< 0) {
3541 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3546 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3549 _PERROR("duplicate stream with ID %" PRId64
,
3555 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3558 "cannot set stream declaration's ID");
3562 _SET(set
, _STREAM_ID_SET
);
3563 } else if (!strcmp(left
, "event.header")) {
3564 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3565 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3570 ret
= visit_type_specifier_list(ctx
,
3571 _BT_LIST_FIRST_ENTRY(
3572 &node
->u
.ctf_expression
.right
,
3573 struct ctf_node
, siblings
),
3576 _PERROR("%s", "cannot create event header declaration");
3582 ret
= bt_ctf_stream_class_set_event_header_type(
3583 stream_class
, decl
);
3586 _PERROR("%s", "cannot set stream's event header declaration");
3590 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3591 } else if (!strcmp(left
, "event.context")) {
3592 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3593 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3598 ret
= visit_type_specifier_list(ctx
,
3599 _BT_LIST_FIRST_ENTRY(
3600 &node
->u
.ctf_expression
.right
,
3601 struct ctf_node
, siblings
),
3604 _PERROR("%s", "cannot create stream event context declaration");
3610 ret
= bt_ctf_stream_class_set_event_context_type(
3611 stream_class
, decl
);
3614 _PERROR("%s", "cannot set stream's event context declaration");
3618 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3619 } else if (!strcmp(left
, "packet.context")) {
3620 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3621 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3626 ret
= visit_type_specifier_list(ctx
,
3627 _BT_LIST_FIRST_ENTRY(
3628 &node
->u
.ctf_expression
.right
,
3629 struct ctf_node
, siblings
),
3632 _PERROR("%s", "cannot create packet context declaration");
3638 ret
= bt_ctf_stream_class_set_packet_context_type(
3639 stream_class
, decl
);
3642 _PERROR("%s", "cannot set stream's packet context declaration");
3646 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3648 _PWARNING("unknown attribute \"%s\" in stream declaration",
3672 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3677 struct ctf_node
*iter
;
3678 struct bt_ctf_stream_class
*stream_class
= NULL
;
3679 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3681 if (node
->visited
) {
3685 node
->visited
= TRUE
;
3686 stream_class
= create_reset_stream_class(ctx
);
3687 if (!stream_class
) {
3692 ret
= ctx_push_scope(ctx
);
3694 _PERROR("%s", "cannot push scope");
3698 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3699 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3708 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3709 /* Check that packet header has stream_id field */
3710 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3711 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3713 packet_header_decl
=
3714 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3715 if (!packet_header_decl
) {
3717 "cannot get trace packet header declaration");
3722 bt_ctf_field_type_structure_get_field_type_by_name(
3723 packet_header_decl
, "stream_id");
3724 BT_PUT(packet_header_decl
);
3725 if (!stream_id_decl
) {
3726 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3730 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3731 BT_PUT(stream_id_decl
);
3732 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3736 BT_PUT(stream_id_decl
);
3738 /* Allow only _one_ ID-less stream */
3739 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3741 "missing \"id\" field in stream declaration");
3746 /* Automatic ID: 0 */
3747 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3750 id
= bt_ctf_stream_class_get_id(stream_class
);
3752 _PERROR("wrong stream ID: %" PRId64
, id
);
3757 /* Move reference to visitor's context */
3758 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3760 stream_class
= NULL
;
3766 BT_PUT(stream_class
);
3772 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3776 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3778 switch (node
->type
) {
3780 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3781 &node
->u
._typedef
.type_declarators
);
3784 "cannot add typedef in \"trace\" declaration");
3788 case NODE_TYPEALIAS
:
3789 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3790 node
->u
.typealias
.alias
);
3793 "cannot add typealias in \"trace\" declaration");
3797 case NODE_CTF_EXPRESSION
:
3799 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3805 if (!strcmp(left
, "major")) {
3806 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3807 _PERROR_DUP_ATTR("major", "trace declaration");
3812 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3815 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3820 _SET(set
, _TRACE_MAJOR_SET
);
3821 } else if (!strcmp(left
, "minor")) {
3822 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3823 _PERROR_DUP_ATTR("minor", "trace declaration");
3828 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3831 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3836 _SET(set
, _TRACE_MINOR_SET
);
3837 } else if (!strcmp(left
, "uuid")) {
3838 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3839 _PERROR_DUP_ATTR("uuid", "trace declaration");
3844 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3848 "invalid trace declaration's UUID");
3852 _SET(set
, _TRACE_UUID_SET
);
3853 } else if (!strcmp(left
, "byte_order")) {
3854 /* Native byte order is already known at this stage */
3855 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3856 _PERROR_DUP_ATTR("byte_order",
3857 "trace declaration");
3862 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3863 } else if (!strcmp(left
, "packet.header")) {
3864 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3865 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3870 ret
= visit_type_specifier_list(ctx
,
3871 _BT_LIST_FIRST_ENTRY(
3872 &node
->u
.ctf_expression
.right
,
3873 struct ctf_node
, siblings
),
3874 &packet_header_decl
);
3876 _PERROR("%s", "cannot create packet header declaration");
3880 assert(packet_header_decl
);
3881 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3882 packet_header_decl
);
3883 BT_PUT(packet_header_decl
);
3885 _PERROR("%s", "cannot set trace declaration's packet header declaration");
3889 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3891 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3899 _PERROR("%s", "unknown expression in trace declaration");
3908 BT_PUT(packet_header_decl
);
3914 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3918 struct ctf_node
*iter
;
3919 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3921 if (node
->visited
) {
3925 node
->visited
= TRUE
;
3927 if (ctx
->is_trace_visited
) {
3928 _PERROR("%s", "duplicate \"trace\" block");
3933 ret
= ctx_push_scope(ctx
);
3935 _PERROR("%s", "cannot push scope");
3939 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3940 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3949 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3951 "missing \"major\" attribute in trace declaration");
3956 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3958 "missing \"minor\" attribute in trace declaration");
3963 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3964 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
3969 ctx
->is_trace_visited
= TRUE
;
3979 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
3983 struct ctf_node
*entry_node
;
3984 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3986 if (node
->visited
) {
3990 node
->visited
= TRUE
;
3992 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
3993 struct bt_list_head
*right_head
=
3994 &entry_node
->u
.ctf_expression
.right
;
3996 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3997 _PERROR("%s", "wrong expression in environment entry");
4002 left
= concatenate_unary_strings(
4003 &entry_node
->u
.ctf_expression
.left
);
4005 _PERROR("%s", "cannot get environment entry name");
4010 if (is_unary_string(right_head
)) {
4011 char *right
= concatenate_unary_strings(right_head
);
4014 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4020 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4021 ret
= bt_ctf_trace_set_environment_field_string(
4022 ctx
->trace
, left
, right
);
4026 _PERROR("environment: cannot add entry \"%s\" to trace",
4030 } else if (is_unary_unsigned(right_head
) ||
4031 is_unary_signed(right_head
)) {
4034 if (is_unary_unsigned(right_head
)) {
4035 ret
= get_unary_unsigned(right_head
,
4038 ret
= get_unary_signed(right_head
, &v
);
4041 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4047 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4048 ret
= bt_ctf_trace_set_environment_field_integer(
4049 ctx
->trace
, left
, v
);
4051 _PERROR("environment: cannot add entry \"%s\" to trace",
4056 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4074 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4079 struct ctf_node
*node
;
4080 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4082 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4083 if (node
->type
== NODE_CTF_EXPRESSION
) {
4084 struct ctf_node
*right_node
;
4086 left
= concatenate_unary_strings(
4087 &node
->u
.ctf_expression
.left
);
4093 if (!strcmp(left
, "byte_order")) {
4094 enum bt_ctf_byte_order bo
;
4096 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4097 _PERROR_DUP_ATTR("byte_order",
4098 "trace declaration");
4103 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4104 right_node
= _BT_LIST_FIRST_ENTRY(
4105 &node
->u
.ctf_expression
.right
,
4106 struct ctf_node
, siblings
);
4107 bo
= byte_order_from_unary_expr(ctx
->efd
,
4109 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4110 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4113 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4114 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4119 ret
= bt_ctf_trace_set_byte_order(
4122 _PERROR("cannot set trace's byte order (%d)",
4133 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4134 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4148 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4149 struct bt_ctf_clock
*clock
, int *set
)
4154 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4159 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4165 if (!strcmp(left
, "name")) {
4168 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4169 _PERROR_DUP_ATTR("name", "clock declaration");
4174 right
= concatenate_unary_strings(
4175 &entry_node
->u
.ctf_expression
.right
);
4177 _PERROR("%s", "unexpected unary expression for clock declaration's \"name\" attribute");
4182 ret
= bt_ctf_clock_set_name(clock
, right
);
4184 _PERROR("%s", "cannot set clock's name");
4190 _SET(set
, _CLOCK_NAME_SET
);
4191 } else if (!strcmp(left
, "uuid")) {
4192 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4194 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4195 _PERROR_DUP_ATTR("uuid", "clock declaration");
4200 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4202 _PERROR("%s", "invalid clock UUID");
4206 ret
= bt_ctf_clock_set_uuid(clock
, uuid
);
4208 _PERROR("%s", "cannot set clock's UUID");
4212 _SET(set
, _CLOCK_UUID_SET
);
4213 } else if (!strcmp(left
, "description")) {
4216 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4217 _PERROR_DUP_ATTR("description", "clock declaration");
4222 right
= concatenate_unary_strings(
4223 &entry_node
->u
.ctf_expression
.right
);
4225 _PERROR("%s", "unexpected unary expression for clock's \"description\" attribute");
4230 ret
= bt_ctf_clock_set_description(clock
, right
);
4232 _PERROR("%s", "cannot set clock's description");
4238 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4239 } else if (!strcmp(left
, "freq")) {
4242 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4243 _PERROR_DUP_ATTR("freq", "clock declaration");
4248 ret
= get_unary_unsigned(
4249 &entry_node
->u
.ctf_expression
.right
, &freq
);
4251 _PERROR("%s", "unexpected unary expression for clock declaration's \"freq\" attribute");
4256 ret
= bt_ctf_clock_set_frequency(clock
, freq
);
4258 _PERROR("%s", "cannot set clock's frequency");
4262 _SET(set
, _CLOCK_FREQ_SET
);
4263 } else if (!strcmp(left
, "precision")) {
4266 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4267 _PERROR_DUP_ATTR("precision", "clock declaration");
4272 ret
= get_unary_unsigned(
4273 &entry_node
->u
.ctf_expression
.right
, &precision
);
4275 _PERROR("%s", "unexpected unary expression for clock declaration's \"precision\" attribute");
4280 ret
= bt_ctf_clock_set_precision(clock
, precision
);
4282 _PERROR("%s", "cannot set clock's precision");
4286 _SET(set
, _CLOCK_PRECISION_SET
);
4287 } else if (!strcmp(left
, "offset_s")) {
4290 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4291 _PERROR_DUP_ATTR("offset_s", "clock declaration");
4296 ret
= get_unary_unsigned(
4297 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4299 _PERROR("%s", "unexpected unary expression for clock declaration's \"offset_s\" attribute");
4304 ret
= bt_ctf_clock_set_offset_s(clock
, offset_s
);
4306 _PERROR("%s", "cannot set clock's offset in seconds");
4310 _SET(set
, _CLOCK_OFFSET_S_SET
);
4311 } else if (!strcmp(left
, "offset")) {
4314 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4315 _PERROR_DUP_ATTR("offset", "clock declaration");
4320 ret
= get_unary_unsigned(
4321 &entry_node
->u
.ctf_expression
.right
, &offset
);
4323 _PERROR("%s", "unexpected unary expression for clock declaration's \"offset\" attribute");
4328 ret
= bt_ctf_clock_set_offset(clock
, offset
);
4330 _PERROR("%s", "cannot set clock's offset in cycles");
4334 _SET(set
, _CLOCK_OFFSET_SET
);
4335 } else if (!strcmp(left
, "absolute")) {
4336 struct ctf_node
*right
;
4338 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4339 _PERROR_DUP_ATTR("absolute", "clock declaration");
4344 right
= _BT_LIST_FIRST_ENTRY(
4345 &entry_node
->u
.ctf_expression
.right
,
4346 struct ctf_node
, siblings
);
4347 ret
= get_boolean(ctx
->efd
, right
);
4349 _PERROR("%s", "unexpected unary expression for clock declaration's \"absolute\" attribute");
4354 ret
= bt_ctf_clock_set_is_absolute(clock
, ret
);
4356 _PERROR("%s", "cannot set clock's absolute option");
4360 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4362 _PWARNING("unknown attribute \"%s\" in clock declaration",
4378 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4382 struct bt_ctf_clock
*clock
;
4383 struct ctf_node
*entry_node
;
4384 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4386 if (clock_node
->visited
) {
4390 clock_node
->visited
= TRUE
;
4391 clock
= bt_ctf_clock_create(NULL
);
4393 _PERROR("%s", "cannot create clock");
4398 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4399 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4405 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4407 "missing \"name\" attribute in clock declaration");
4412 if (bt_ctf_trace_get_clock_count(ctx
->trace
) != 0) {
4413 _PERROR("%s", "only CTF traces with a single clock declaration are supported as of this version");
4418 ret
= bt_ctf_trace_add_clock(ctx
->trace
, clock
);
4420 _PERROR("%s", "cannot add clock to trace");
4431 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4435 if (root_decl_node
->visited
) {
4439 root_decl_node
->visited
= TRUE
;
4441 switch (root_decl_node
->type
) {
4443 ret
= visit_typedef(ctx
,
4444 root_decl_node
->u
._typedef
.type_specifier_list
,
4445 &root_decl_node
->u
._typedef
.type_declarators
);
4447 _PERROR("%s", "cannot add typedef in root scope");
4451 case NODE_TYPEALIAS
:
4452 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4453 root_decl_node
->u
.typealias
.alias
);
4455 _PERROR("%s", "cannot add typealias in root scope");
4459 case NODE_TYPE_SPECIFIER_LIST
:
4461 _BT_CTF_FIELD_TYPE_INIT(decl
);
4464 * Just add the type specifier to the root
4465 * declaration scope. Put local reference.
4467 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4486 int add_stream_classes_to_trace(struct ctx
*ctx
)
4489 GHashTableIter iter
;
4490 gpointer key
, stream_class
;
4492 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4494 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4495 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4498 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4499 _PERROR("cannot add stream class %" PRId64
" to trace",
4509 int ctf_visitor_generate_ir(FILE *efd
, struct ctf_node
*node
,
4510 struct bt_ctf_trace
**trace
)
4513 struct ctx
*ctx
= NULL
;
4515 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4517 *trace
= bt_ctf_trace_create();
4519 _FPERROR(efd
, "%s", "cannot create trace");
4524 /* Set packet header to NULL to override the default one */
4525 ret
= bt_ctf_trace_set_packet_header_type(*trace
, NULL
);
4529 "cannot set initial, empty packet header structure");
4533 ctx
= ctx_create(*trace
, efd
);
4535 _FPERROR(efd
, "%s", "cannot create visitor context");
4540 switch (node
->type
) {
4543 struct ctf_node
*iter
;
4544 int got_trace_decl
= FALSE
;
4545 int found_callsite
= FALSE
;
4548 * Find trace declaration's byte order first (for early
4551 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4552 if (got_trace_decl
) {
4553 _PERROR("%s", "duplicate trace declaration");
4557 ret
= set_trace_byte_order(ctx
, iter
);
4559 _PERROR("cannot set trace's byte order (%d)",
4564 got_trace_decl
= TRUE
;
4567 if (!got_trace_decl
) {
4568 _PERROR("no trace declaration found (%d)", ret
);
4574 * Visit clocks first since any early integer can be mapped
4577 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4578 ret
= visit_clock_decl(ctx
, iter
);
4580 _PERROR("error while visiting clock declaration (%d)",
4587 * Visit root declarations next, as they can be used by any
4590 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4592 ret
= visit_root_decl(ctx
, iter
);
4594 _PERROR("error while visiting root declaration (%d)",
4600 /* Callsite are not supported */
4601 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4602 found_callsite
= TRUE
;
4606 if (found_callsite
) {
4607 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4611 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4612 ret
= visit_env(ctx
, iter
);
4614 _PERROR("error while visiting environment block (%d)",
4621 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4622 ret
= visit_trace_decl(ctx
, iter
);
4624 _PERROR("%s", "error while visiting trace declaration");
4630 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4631 ret
= visit_stream_decl(ctx
, iter
);
4633 _PERROR("%s", "error while visiting stream declaration");
4639 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4640 ret
= visit_event_decl(ctx
, iter
);
4642 _PERROR("%s", "error while visiting event declaration");
4650 _PERROR("unknown node type: %d", (int) node
->type
);
4655 /* Add stream classes to trace now */
4656 ret
= add_stream_classes_to_trace(ctx
);
4658 _PERROR("%s", "cannot add stream classes to trace");
4662 printf_verbose("done!\n");