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 if (min_align_value
!= 0) {
1822 ret
= bt_ctf_field_type_set_alignment(*struct_decl
,
1825 _PERROR("%s", "failed to set structure's minimal alignment");
1830 ret
= ctx_push_scope(ctx
);
1832 _PERROR("%s", "cannot push scope");
1836 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1837 ret
= visit_struct_decl_entry(ctx
, entry_node
,
1848 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
,
1849 name
, *struct_decl
);
1851 _PERROR("cannot register \"struct %s\" in declaration scope",
1861 BT_PUT(*struct_decl
);
1867 int visit_variant_decl(struct ctx
*ctx
, const char *name
,
1868 const char *tag
, struct bt_list_head
*decl_list
,
1869 int has_body
, struct bt_ctf_field_type
**variant_decl
)
1872 _BT_CTF_FIELD_TYPE_INIT(untagged_variant_decl
);
1874 *variant_decl
= NULL
;
1876 /* For named variant (without body), lookup in declaration scope */
1878 _BT_CTF_FIELD_TYPE_INIT(variant_decl_copy
);
1885 untagged_variant_decl
=
1886 ctx_decl_scope_lookup_variant(ctx
->current_scope
,
1888 if (!untagged_variant_decl
) {
1889 _PERROR("cannot find \"variant %s\"", name
);
1894 /* Make a copy of it */
1895 variant_decl_copy
= bt_ctf_field_type_copy(
1896 untagged_variant_decl
);
1897 if (!variant_decl_copy
) {
1899 "cannot create copy of structure declaration");
1904 BT_MOVE(untagged_variant_decl
, variant_decl_copy
);
1906 struct ctf_node
*entry_node
;
1909 struct bt_ctf_field_type
*evariant_decl
=
1910 ctx_decl_scope_lookup_struct(ctx
->current_scope
,
1913 if (evariant_decl
) {
1914 BT_PUT(evariant_decl
);
1915 _PERROR("\"variant %s\" already declared in local scope",
1922 untagged_variant_decl
= bt_ctf_field_type_variant_create(NULL
,
1924 if (!untagged_variant_decl
) {
1925 _PERROR("%s", "cannot create variant declaration");
1930 ret
= ctx_push_scope(ctx
);
1932 _PERROR("%s", "cannot push scope");
1936 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
1937 ret
= visit_variant_decl_entry(ctx
, entry_node
,
1938 untagged_variant_decl
);
1948 ret
= ctx_decl_scope_register_variant(
1949 ctx
->current_scope
, name
,
1950 untagged_variant_decl
);
1952 _PERROR("cannot register \"variant %s\" in declaration scope",
1960 * If tagged, create tagged variant and return; otherwise
1961 * return untagged variant.
1964 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1967 * At this point, we have a fresh untagged variant; nobody
1968 * else owns it. Set its tag now.
1970 ret
= bt_ctf_field_type_variant_set_tag_name(
1971 untagged_variant_decl
, tag
);
1976 BT_MOVE(*variant_decl
, untagged_variant_decl
);
1979 assert(!untagged_variant_decl
);
1980 assert(*variant_decl
);
1985 BT_PUT(untagged_variant_decl
);
1986 BT_PUT(*variant_decl
);
1992 int visit_enum_decl_entry(struct ctx
*ctx
, struct ctf_node
*enumerator
,
1993 struct bt_ctf_field_type
*enum_decl
, int64_t *last
, int is_signed
)
1997 struct ctf_node
*iter
;
1998 int64_t start
= 0, end
= 0;
1999 const char *label
= enumerator
->u
.enumerator
.id
;
2000 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
2002 bt_list_for_each_entry(iter
, values
, siblings
) {
2005 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
2006 _PERROR("wrong unary expression for enumeration label \"%s\"",
2018 switch (iter
->u
.unary_expression
.type
) {
2019 case UNARY_SIGNED_CONSTANT
:
2020 *target
= iter
->u
.unary_expression
.u
.signed_constant
;
2022 case UNARY_UNSIGNED_CONSTANT
:
2024 iter
->u
.unary_expression
.u
.unsigned_constant
;
2027 _PERROR("invalid enumeration entry: \"%s\"",
2034 _PERROR("invalid enumeration entry: \"%s\"",
2054 ret
= bt_ctf_field_type_enumeration_add_mapping(enum_decl
, label
,
2057 ret
= bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_decl
,
2058 label
, (uint64_t) start
, (uint64_t) end
);
2061 _PERROR("cannot add mapping to enumeration for label \"%s\"",
2073 int visit_enum_decl(struct ctx
*ctx
, const char *name
,
2074 struct ctf_node
*container_type
,
2075 struct bt_list_head
*enumerator_list
,
2077 struct bt_ctf_field_type
**enum_decl
)
2081 _BT_CTF_FIELD_TYPE_INIT(integer_decl
);
2085 /* For named enum (without body), lookup in declaration scope */
2087 _BT_CTF_FIELD_TYPE_INIT(enum_decl_copy
);
2094 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
,
2097 _PERROR("cannot find \"enum %s\"", name
);
2102 /* Make a copy of it */
2103 enum_decl_copy
= bt_ctf_field_type_copy(*enum_decl
);
2104 if (!enum_decl_copy
) {
2106 "cannot create copy of enumeration declaration");
2112 BT_MOVE(*enum_decl
, enum_decl_copy
);
2114 struct ctf_node
*iter
;
2115 int64_t last_value
= 0;
2118 _BT_CTF_FIELD_TYPE_INIT(eenum_decl
);
2120 eenum_decl
= ctx_decl_scope_lookup_enum(
2121 ctx
->current_scope
, name
, 1);
2124 _PERROR("\"enum %s\" already declared in local scope",
2131 if (!container_type
) {
2132 integer_decl
= ctx_decl_scope_lookup_alias(
2133 ctx
->current_scope
, "int", -1);
2134 if (!integer_decl
) {
2135 _PERROR("%s", "cannot find \"int\" type for enumeration");
2140 ret
= visit_type_declarator(ctx
, container_type
,
2141 &qdummy_id
, NULL
, &integer_decl
, NULL
);
2143 assert(!integer_decl
);
2149 assert(integer_decl
);
2151 if (!bt_ctf_field_type_is_integer(integer_decl
)) {
2152 _PERROR("%s", "container type for enumeration is not an integer");
2157 *enum_decl
= bt_ctf_field_type_enumeration_create(integer_decl
);
2159 _PERROR("%s", "cannot create enumeration declaration");
2164 bt_list_for_each_entry(iter
, enumerator_list
, siblings
) {
2165 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
,
2167 bt_ctf_field_type_integer_get_signed(integer_decl
));
2174 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
,
2182 BT_PUT(integer_decl
);
2187 BT_PUT(integer_decl
);
2194 int visit_type_specifier(struct ctx
*ctx
,
2195 struct ctf_node
*type_specifier_list
,
2196 struct bt_ctf_field_type
**decl
)
2199 GString
*str
= NULL
;
2200 _BT_CTF_FIELD_TYPE_INIT(decl_copy
);
2203 str
= g_string_new("");
2204 ret
= get_type_specifier_list_name(ctx
, type_specifier_list
, str
);
2209 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1);
2211 _PERROR("cannot find type alias \"%s\"", str
->str
);
2216 /* Make a copy of the type declaration */
2217 decl_copy
= bt_ctf_field_type_copy(*decl
);
2219 _PERROR("%s", "cannot create copy of type declaration");
2224 BT_MOVE(*decl
, decl_copy
);
2225 (void) g_string_free(str
, TRUE
);
2232 (void) g_string_free(str
, TRUE
);
2241 int visit_integer_decl(struct ctx
*ctx
,
2242 struct bt_list_head
*expressions
,
2243 struct bt_ctf_field_type
**integer_decl
)
2248 struct ctf_node
*expression
;
2249 uint64_t alignment
= 0, size
= 0;
2250 struct bt_ctf_clock
*mapped_clock
= NULL
;
2251 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_NONE
;
2252 enum bt_ctf_integer_base base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2253 enum bt_ctf_byte_order byte_order
=
2254 bt_ctf_trace_get_byte_order(ctx
->trace
);
2256 *integer_decl
= NULL
;
2258 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2259 struct ctf_node
*left
, *right
;
2261 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2262 struct ctf_node
, siblings
);
2263 right
= _BT_LIST_FIRST_ENTRY(
2264 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2267 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2272 if (!strcmp(left
->u
.unary_expression
.u
.string
, "signed")) {
2273 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2274 _PERROR_DUP_ATTR("signed",
2275 "integer declaration");
2280 signedness
= get_boolean(ctx
->efd
, right
);
2281 if (signedness
< 0) {
2286 _SET(&set
, _INTEGER_SIGNED_SET
);
2287 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2289 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2290 _PERROR_DUP_ATTR("byte_order",
2291 "integer declaration");
2296 byte_order
= get_real_byte_order(ctx
, right
);
2297 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2298 _PERROR("%s", "invalid \"byte_order\" attribute in integer declaration");
2303 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2304 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "size")) {
2305 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2306 _PERROR_DUP_ATTR("size",
2307 "integer declaration");
2312 if (right
->u
.unary_expression
.type
!=
2313 UNARY_UNSIGNED_CONSTANT
) {
2314 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting unsigned constant");
2319 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2321 _PERROR("%s", "invalid \"size\" attribute in integer declaration: expecting positive constant");
2324 } else if (size
> 64) {
2325 _PERROR("%s", "invalid \"size\" attribute in integer declaration: integers over 64-bit are not supported as of this version");
2330 _SET(&set
, _INTEGER_SIZE_SET
);
2331 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2333 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2334 _PERROR_DUP_ATTR("align",
2335 "integer declaration");
2340 if (right
->u
.unary_expression
.type
!=
2341 UNARY_UNSIGNED_CONSTANT
) {
2342 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting unsigned constant");
2348 right
->u
.unary_expression
.u
.unsigned_constant
;
2349 if (!is_align_valid(alignment
)) {
2350 _PERROR("%s", "invalid \"align\" attribute in integer declaration: expecting power of two");
2355 _SET(&set
, _INTEGER_ALIGN_SET
);
2356 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "base")) {
2357 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2358 _PERROR_DUP_ATTR("base", "integer declaration");
2363 switch (right
->u
.unary_expression
.type
) {
2364 case UNARY_UNSIGNED_CONSTANT
:
2366 uint64_t constant
= right
->u
.unary_expression
.
2367 u
.unsigned_constant
;
2371 base
= BT_CTF_INTEGER_BASE_BINARY
;
2374 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2377 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2380 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2383 _PERROR("invalid \"base\" attribute in integer declaration: %" PRIu64
,
2384 right
->u
.unary_expression
.u
.unsigned_constant
);
2392 char *s_right
= concatenate_unary_strings(
2393 &expression
->u
.ctf_expression
.right
);
2395 _PERROR("%s", "unexpected unary expression for integer declaration's \"base\" attribute");
2400 if (!strcmp(s_right
, "decimal") ||
2401 !strcmp(s_right
, "dec") ||
2402 !strcmp(s_right
, "d") ||
2403 !strcmp(s_right
, "i") ||
2404 !strcmp(s_right
, "u")) {
2405 base
= BT_CTF_INTEGER_BASE_DECIMAL
;
2406 } else if (!strcmp(s_right
, "hexadecimal") ||
2407 !strcmp(s_right
, "hex") ||
2408 !strcmp(s_right
, "x") ||
2409 !strcmp(s_right
, "X") ||
2410 !strcmp(s_right
, "p")) {
2411 base
= BT_CTF_INTEGER_BASE_HEXADECIMAL
;
2412 } else if (!strcmp(s_right
, "octal") ||
2413 !strcmp(s_right
, "oct") ||
2414 !strcmp(s_right
, "o")) {
2415 base
= BT_CTF_INTEGER_BASE_OCTAL
;
2416 } else if (!strcmp(s_right
, "binary") ||
2417 !strcmp(s_right
, "b")) {
2418 base
= BT_CTF_INTEGER_BASE_BINARY
;
2420 _PERROR("unexpected unary expression for integer declaration's \"base\" attribute: \"%s\"",
2431 _PERROR("%s", "invalid \"base\" attribute in integer declaration: expecting unsigned constant or unary string");
2436 _SET(&set
, _INTEGER_BASE_SET
);
2437 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2441 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2442 _PERROR_DUP_ATTR("encoding",
2443 "integer declaration");
2448 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2449 _PERROR("%s", "invalid \"encoding\" attribute in integer declaration: expecting unary string");
2454 s_right
= concatenate_unary_strings(
2455 &expression
->u
.ctf_expression
.right
);
2457 _PERROR("%s", "unexpected unary expression for integer declaration's \"encoding\" attribute");
2462 if (!strcmp(s_right
, "UTF8") ||
2463 !strcmp(s_right
, "utf8") ||
2464 !strcmp(s_right
, "utf-8") ||
2465 !strcmp(s_right
, "UTF-8")) {
2466 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2467 } else if (!strcmp(s_right
, "ASCII") ||
2468 !strcmp(s_right
, "ascii")) {
2469 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2470 } else if (!strcmp(s_right
, "none")) {
2471 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2473 _PERROR("invalid \"encoding\" attribute in integer declaration: unknown encoding \"%s\"",
2481 _SET(&set
, _INTEGER_ENCODING_SET
);
2482 } else if (!strcmp(left
->u
.unary_expression
.u
.string
, "map")) {
2483 const char *clock_name
;
2485 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2486 _PERROR_DUP_ATTR("map", "integer declaration");
2491 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2492 _PERROR("%s", "invalid \"map\" attribute in integer declaration: expecting unary string");
2498 get_map_clock_name_value(
2499 &expression
->u
.ctf_expression
.right
);
2501 char *s_right
= concatenate_unary_strings(
2502 &expression
->u
.ctf_expression
.right
);
2505 _PERROR("%s", "unexpected unary expression for integer declaration's \"map\" attribute");
2510 _PWARNING("invalid \"map\" attribute in integer declaration: unknown clock: \"%s\"",
2512 _SET(&set
, _INTEGER_MAP_SET
);
2517 mapped_clock
= bt_ctf_trace_get_clock_by_name(
2518 ctx
->trace
, clock_name
);
2519 if (!mapped_clock
) {
2520 _PERROR("invalid \"map\" attribute in integer declaration: cannot find clock \"%s\"",
2526 _SET(&set
, _INTEGER_MAP_SET
);
2528 _PWARNING("unknown attribute \"%s\" in integer declaration",
2529 left
->u
.unary_expression
.u
.string
);
2533 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2535 "missing \"size\" attribute in integer declaration");
2540 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2541 if (size
% CHAR_BIT
) {
2542 /* Bit-packed alignment */
2545 /* Byte-packed alignment */
2546 alignment
= CHAR_BIT
;
2550 *integer_decl
= bt_ctf_field_type_integer_create((unsigned int) size
);
2551 if (!*integer_decl
) {
2552 _PERROR("%s", "cannot create integer declaration");
2557 ret
= bt_ctf_field_type_integer_set_signed(*integer_decl
, signedness
);
2558 ret
|= bt_ctf_field_type_integer_set_base(*integer_decl
, base
);
2559 ret
|= bt_ctf_field_type_integer_set_encoding(*integer_decl
, encoding
);
2560 ret
|= bt_ctf_field_type_set_alignment(*integer_decl
,
2561 (unsigned int) alignment
);
2562 ret
|= bt_ctf_field_type_set_byte_order(*integer_decl
, byte_order
);
2566 ret
|= bt_ctf_field_type_integer_set_mapped_clock(
2567 *integer_decl
, mapped_clock
);
2568 bt_put(mapped_clock
);
2569 mapped_clock
= NULL
;
2573 _PERROR("%s", "cannot configure integer declaration");
2582 bt_put(mapped_clock
);
2585 BT_PUT(*integer_decl
);
2591 int visit_floating_point_number_decl(struct ctx
*ctx
,
2592 struct bt_list_head
*expressions
,
2593 struct bt_ctf_field_type
**float_decl
)
2597 struct ctf_node
*expression
;
2598 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2599 enum bt_ctf_byte_order byte_order
=
2600 bt_ctf_trace_get_byte_order(ctx
->trace
);
2604 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2605 struct ctf_node
*left
, *right
;
2607 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2608 struct ctf_node
, siblings
);
2609 right
= _BT_LIST_FIRST_ENTRY(
2610 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2613 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2618 if (!strcmp(left
->u
.unary_expression
.u
.string
, "byte_order")) {
2619 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2620 _PERROR_DUP_ATTR("byte_order",
2621 "floating point number declaration");
2626 byte_order
= get_real_byte_order(ctx
, right
);
2627 if (byte_order
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
2628 _PERROR("%s", "invalid \"byte_order\" attribute in floating point number declaration");
2633 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2634 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2636 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2637 _PERROR_DUP_ATTR("exp_dig",
2638 "floating point number declaration");
2643 if (right
->u
.unary_expression
.type
!=
2644 UNARY_UNSIGNED_CONSTANT
) {
2645 _PERROR("%s", "invalid \"exp_dig\" attribute in floating point number declaration: expecting unsigned constant");
2650 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2651 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2652 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2654 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2655 _PERROR_DUP_ATTR("mant_dig",
2656 "floating point number declaration");
2661 if (right
->u
.unary_expression
.type
!=
2662 UNARY_UNSIGNED_CONSTANT
) {
2663 _PERROR("%s", "invalid \"mant_dig\" attribute in floating point number declaration: expecting unsigned constant");
2668 mant_dig
= right
->u
.unary_expression
.u
.
2670 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2671 } else if (!strcmp(left
->u
.unary_expression
.u
.string
,
2673 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2674 _PERROR_DUP_ATTR("align",
2675 "floating point number declaration");
2680 if (right
->u
.unary_expression
.type
!=
2681 UNARY_UNSIGNED_CONSTANT
) {
2682 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting unsigned constant");
2687 alignment
= right
->u
.unary_expression
.u
.
2690 if (!is_align_valid(alignment
)) {
2691 _PERROR("%s", "invalid \"align\" attribute in floating point number declaration: expecting power of two");
2696 _SET(&set
, _FLOAT_ALIGN_SET
);
2698 _PWARNING("unknown attribute \"%s\" in floating point number declaration",
2699 left
->u
.unary_expression
.u
.string
);
2703 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2704 _PERROR("%s", "missing \"mant_dig\" attribute in floating point number declaration");
2709 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2710 _PERROR("%s", "missing \"exp_dig\" attribute in floating point number declaration");
2715 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2716 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2717 /* Bit-packed alignment */
2720 /* Byte-packed alignment */
2721 alignment
= CHAR_BIT
;
2725 *float_decl
= bt_ctf_field_type_floating_point_create();
2728 "cannot create floating point number declaration");
2733 ret
= bt_ctf_field_type_floating_point_set_exponent_digits(
2734 *float_decl
, exp_dig
);
2735 ret
|= bt_ctf_field_type_floating_point_set_mantissa_digits(
2736 *float_decl
, mant_dig
);
2737 ret
|= bt_ctf_field_type_set_byte_order(*float_decl
, byte_order
);
2738 ret
|= bt_ctf_field_type_set_alignment(*float_decl
, alignment
);
2741 "cannot configure floating point number declaration");
2749 BT_PUT(*float_decl
);
2755 int visit_string_decl(struct ctx
*ctx
,
2756 struct bt_list_head
*expressions
,
2757 struct bt_ctf_field_type
**string_decl
)
2761 struct ctf_node
*expression
;
2762 enum bt_ctf_string_encoding encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2764 *string_decl
= NULL
;
2766 bt_list_for_each_entry(expression
, expressions
, siblings
) {
2767 struct ctf_node
*left
, *right
;
2769 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
,
2770 struct ctf_node
, siblings
);
2771 right
= _BT_LIST_FIRST_ENTRY(
2772 &expression
->u
.ctf_expression
.right
, struct ctf_node
,
2775 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2780 if (!strcmp(left
->u
.unary_expression
.u
.string
, "encoding")) {
2783 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2784 _PERROR_DUP_ATTR("encoding",
2785 "string declaration");
2790 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2791 _PERROR("%s", "invalid \"encoding\" attribute in string declaration: expecting unary string");
2796 s_right
= concatenate_unary_strings(
2797 &expression
->u
.ctf_expression
.right
);
2799 _PERROR("%s", "unexpected unary expression for string declaration's \"encoding\" attribute");
2804 if (!strcmp(s_right
, "UTF8") ||
2805 !strcmp(s_right
, "utf8") ||
2806 !strcmp(s_right
, "utf-8") ||
2807 !strcmp(s_right
, "UTF-8")) {
2808 encoding
= BT_CTF_STRING_ENCODING_UTF8
;
2809 } else if (!strcmp(s_right
, "ASCII") ||
2810 !strcmp(s_right
, "ascii")) {
2811 encoding
= BT_CTF_STRING_ENCODING_ASCII
;
2812 } else if (!strcmp(s_right
, "none")) {
2813 encoding
= BT_CTF_STRING_ENCODING_NONE
;
2815 _PERROR("invalid \"encoding\" attribute in string declaration: unknown encoding \"%s\"",
2823 _SET(&set
, _STRING_ENCODING_SET
);
2825 _PWARNING("unknown attribute \"%s\" in string declaration",
2826 left
->u
.unary_expression
.u
.string
);
2830 *string_decl
= bt_ctf_field_type_string_create();
2831 if (!*string_decl
) {
2832 _PERROR("%s", "cannot create string declaration");
2837 ret
= bt_ctf_field_type_string_set_encoding(*string_decl
, encoding
);
2839 _PERROR("%s", "cannot configure string declaration");
2847 BT_PUT(*string_decl
);
2853 int visit_type_specifier_list(struct ctx
*ctx
,
2854 struct ctf_node
*ts_list
,
2855 struct bt_ctf_field_type
**decl
)
2858 struct ctf_node
*first
, *node
;
2862 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2867 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.type_specifier_list
.head
,
2868 struct ctf_node
, siblings
);
2869 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2874 node
= first
->u
.type_specifier
.node
;
2876 switch (first
->u
.type_specifier
.type
) {
2877 case TYPESPEC_INTEGER
:
2878 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
,
2885 case TYPESPEC_FLOATING_POINT
:
2886 ret
= visit_floating_point_number_decl(ctx
,
2887 &node
->u
.floating_point
.expressions
, decl
);
2893 case TYPESPEC_STRING
:
2894 ret
= visit_string_decl(ctx
,
2895 &node
->u
.string
.expressions
, decl
);
2901 case TYPESPEC_STRUCT
:
2902 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
,
2903 &node
->u
._struct
.declaration_list
,
2904 node
->u
._struct
.has_body
,
2905 &node
->u
._struct
.min_align
, decl
);
2911 case TYPESPEC_VARIANT
:
2912 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
,
2913 node
->u
.variant
.choice
,
2914 &node
->u
.variant
.declaration_list
,
2915 node
->u
.variant
.has_body
, decl
);
2922 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
,
2923 node
->u
._enum
.container_type
,
2924 &node
->u
._enum
.enumerator_list
,
2925 node
->u
._enum
.has_body
, decl
);
2933 case TYPESPEC_SHORT
:
2936 case TYPESPEC_FLOAT
:
2937 case TYPESPEC_DOUBLE
:
2938 case TYPESPEC_SIGNED
:
2939 case TYPESPEC_UNSIGNED
:
2941 case TYPESPEC_COMPLEX
:
2942 case TYPESPEC_IMAGINARY
:
2943 case TYPESPEC_CONST
:
2944 case TYPESPEC_ID_TYPE
:
2945 ret
= visit_type_specifier(ctx
, ts_list
, decl
);
2952 _PERROR("unexpected node type: %d",
2953 (int) first
->u
.type_specifier
.type
);
2969 int visit_event_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
2970 struct bt_ctf_event_class
*event_class
, int64_t *stream_id
,
2975 _BT_CTF_FIELD_TYPE_INIT(decl
);
2977 switch (node
->type
) {
2979 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
2980 &node
->u
._typedef
.type_declarators
);
2983 "cannot add typedef in \"event\" declaration");
2987 case NODE_TYPEALIAS
:
2988 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
2989 node
->u
.typealias
.alias
);
2991 _PERROR("%s", "cannot add typealias in \"event\" declaration");
2995 case NODE_CTF_EXPRESSION
:
2997 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3003 if (!strcmp(left
, "name")) {
3004 /* This is already known at this stage */
3005 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
3006 _PERROR_DUP_ATTR("name", "event declaration");
3011 _SET(set
, _EVENT_NAME_SET
);
3012 } else if (!strcmp(left
, "id")) {
3015 if (_IS_SET(set
, _EVENT_ID_SET
)) {
3016 _PERROR_DUP_ATTR("id", "event declaration");
3021 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3023 if (ret
|| id
< 0) {
3024 _PERROR("%s", "unexpected unary expression for event declaration's \"id\" attribute");
3029 ret
= bt_ctf_event_class_set_id(event_class
, id
);
3032 "cannot set event declaration's ID");
3036 _SET(set
, _EVENT_ID_SET
);
3037 } else if (!strcmp(left
, "stream_id")) {
3038 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
3039 _PERROR_DUP_ATTR("stream_id",
3040 "event declaration");
3045 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3046 (uint64_t *) stream_id
);
3047 if (ret
|| *stream_id
< 0) {
3048 _PERROR("%s", "unexpected unary expression for event declaration's \"stream_id\" attribute");
3053 _SET(set
, _EVENT_STREAM_ID_SET
);
3054 } else if (!strcmp(left
, "context")) {
3055 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
3056 _PERROR("%s", "duplicate \"context\" entry in event declaration");
3061 ret
= visit_type_specifier_list(ctx
,
3062 _BT_LIST_FIRST_ENTRY(
3063 &node
->u
.ctf_expression
.right
,
3064 struct ctf_node
, siblings
),
3067 _PERROR("%s", "cannot create event context declaration");
3072 ret
= bt_ctf_event_class_set_context_type(
3076 _PERROR("%s", "cannot set event's context declaration");
3080 _SET(set
, _EVENT_CONTEXT_SET
);
3081 } else if (!strcmp(left
, "fields")) {
3082 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
3083 _PERROR("%s", "duplicate \"fields\" entry in event declaration");
3088 ret
= visit_type_specifier_list(ctx
,
3089 _BT_LIST_FIRST_ENTRY(
3090 &node
->u
.ctf_expression
.right
,
3091 struct ctf_node
, siblings
),
3094 _PERROR("%s", "cannot create event payload declaration");
3099 ret
= bt_ctf_event_class_set_payload_type(
3103 _PERROR("%s", "cannot set event's payload declaration");
3107 _SET(set
, _EVENT_FIELDS_SET
);
3108 } else if (!strcmp(left
, "loglevel")) {
3109 uint64_t loglevel_value
;
3110 const char *loglevel_str
;
3111 struct bt_value
*value_obj
, *str_obj
;
3113 if (_IS_SET(set
, _EVENT_LOGLEVEL_SET
)) {
3114 _PERROR_DUP_ATTR("loglevel",
3115 "event declaration");
3120 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3123 _PERROR("%s", "unexpected unary expression for event declaration's \"loglevel\" attribute");
3127 value_obj
= bt_value_integer_create_init(loglevel_value
);
3129 _PERROR("%s", "cannot allocate memory for loglevel value object");
3133 if (bt_ctf_event_class_set_attribute(event_class
,
3134 "loglevel", value_obj
) != BT_VALUE_STATUS_OK
) {
3135 _PERROR("%s", "cannot set loglevel value");
3140 loglevel_str
= print_loglevel(loglevel_value
);
3142 str_obj
= bt_value_string_create_init(loglevel_str
);
3143 if (bt_ctf_event_class_set_attribute(event_class
,
3144 "loglevel_string", str_obj
) != BT_VALUE_STATUS_OK
) {
3145 _PERROR("%s", "cannot set loglevel string");
3153 _SET(set
, _EVENT_LOGLEVEL_SET
);
3154 } else if (!strcmp(left
, "model.emf.uri")) {
3157 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3158 _PERROR_DUP_ATTR("model.emf.uri",
3159 "event declaration");
3164 right
= concatenate_unary_strings(
3165 &node
->u
.ctf_expression
.right
);
3167 _PERROR("%s", "unexpected unary expression for event declaration's \"model.emf.uri\" attribute");
3172 // TODO: FIXME: set model EMF URI here
3175 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3177 _PWARNING("unknown attribute \"%s\" in event declaration",
3203 char *get_event_decl_name(struct ctx
*ctx
, struct ctf_node
*node
)
3207 struct ctf_node
*iter
;
3208 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3210 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3211 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3215 left
= concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3220 if (!strcmp(left
, "name")) {
3221 name
= concatenate_unary_strings(
3222 &iter
->u
.ctf_expression
.right
);
3224 _PERROR("%s", "unexpected unary expression for event declaration's \"name\" attribute");
3246 int reset_event_decl_types(struct ctx
*ctx
,
3247 struct bt_ctf_event_class
*event_class
)
3252 ret
= bt_ctf_event_class_set_context_type(event_class
, NULL
);
3254 _PERROR("%s", "cannot set initial NULL event context");
3258 /* Event payload. */
3259 ret
= bt_ctf_event_class_set_payload_type(event_class
, NULL
);
3261 _PERROR("%s", "cannot set initial NULL event payload");
3269 int reset_stream_decl_types(struct ctx
*ctx
,
3270 struct bt_ctf_stream_class
*stream_class
)
3274 /* Packet context. */
3275 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
3277 _PERROR("%s", "cannot set initial empty packet context");
3282 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
3284 _PERROR("%s", "cannot set initial empty event header");
3288 /* Event context. */
3289 ret
= bt_ctf_stream_class_set_event_context_type(stream_class
, NULL
);
3291 _PERROR("%s", "cannot set initial empty stream event context");
3299 struct bt_ctf_stream_class
*create_reset_stream_class(struct ctx
*ctx
)
3302 struct bt_ctf_stream_class
*stream_class
;
3304 stream_class
= bt_ctf_stream_class_create(NULL
);
3305 if (!stream_class
) {
3306 _PERROR("%s", "cannot create stream class");
3311 * Set packet context, event header, and event context to NULL to
3312 * override the default ones.
3314 ret
= reset_stream_decl_types(ctx
, stream_class
);
3319 return stream_class
;
3322 BT_PUT(stream_class
);
3328 int visit_event_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3333 struct ctf_node
*iter
;
3334 int64_t stream_id
= -1;
3335 char *event_name
= NULL
;
3336 struct bt_ctf_event_class
*event_class
= NULL
;
3337 struct bt_ctf_event_class
*eevent_class
;
3338 struct bt_ctf_stream_class
*stream_class
;
3339 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3341 if (node
->visited
) {
3345 node
->visited
= TRUE
;
3346 event_name
= get_event_decl_name(ctx
, node
);
3349 "missing \"name\" attribute in event declaration");
3354 event_class
= bt_ctf_event_class_create(event_name
);
3357 * Unset context and fields to override the default ones.
3359 ret
= reset_event_decl_types(ctx
, event_class
);
3365 ret
= ctx_push_scope(ctx
);
3367 _PERROR("%s", "cannot push scope");
3371 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3372 ret
= visit_event_decl_entry(ctx
, iter
, event_class
,
3379 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3381 struct bt_ctf_stream_class
*new_stream_class
;
3383 /* Allow missing stream_id if there is only a single stream */
3384 switch (g_hash_table_size(ctx
->stream_classes
)) {
3386 /* Create stream if there's none */
3387 new_stream_class
= create_reset_stream_class(ctx
);
3388 if (!new_stream_class
) {
3393 ret
= bt_ctf_stream_class_set_id(new_stream_class
, 0);
3395 _PERROR("%s", "cannot set stream's ID");
3396 BT_PUT(new_stream_class
);
3402 /* Move reference to visitor's context */
3403 g_hash_table_insert(ctx
->stream_classes
,
3404 (gpointer
) stream_id
, new_stream_class
);
3405 new_stream_class
= NULL
;
3409 /* Single stream: get its ID */
3410 keys
= g_hash_table_get_keys(ctx
->stream_classes
);
3411 stream_id
= (int64_t) keys
->data
;
3416 _PERROR("%s", "missing \"stream_id\" attribute in event declaration");
3424 assert(stream_id
>= 0);
3426 /* We have the stream ID now; borrow the stream class if found */
3427 stream_class
= g_hash_table_lookup(ctx
->stream_classes
,
3428 (gpointer
) stream_id
);
3429 if (!stream_class
) {
3430 _PERROR("cannot find stream class with ID %" PRId64
,
3436 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3437 /* Allow only one event without ID per stream */
3438 if (bt_ctf_stream_class_get_event_class_count(stream_class
) !=
3441 "missing \"id\" field in event declaration");
3447 ret
= bt_ctf_event_class_set_id(event_class
, 0);
3449 _PERROR("%s", "cannot set event's ID");
3454 event_id
= bt_ctf_event_class_get_id(event_class
);
3456 _PERROR("%s", "cannot get event's ID");
3461 eevent_class
= bt_ctf_stream_class_get_event_class_by_id(stream_class
,
3464 BT_PUT(eevent_class
);
3465 _PERROR("%s", "duplicate event with ID %" PRId64
" in same stream");
3470 eevent_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
,
3473 BT_PUT(eevent_class
);
3474 eevent_class
= NULL
;
3476 "duplicate event with name \"%s\" in same stream");
3482 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
3483 BT_PUT(event_class
);
3487 _PERROR("%s", "cannot add event class to stream class");
3496 BT_PUT(event_class
);
3498 /* stream_class is borrowed; it still belongs to the hash table */
3504 int visit_stream_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
,
3505 struct bt_ctf_stream_class
*stream_class
, int *set
)
3509 _BT_CTF_FIELD_TYPE_INIT(decl
);
3511 switch (node
->type
) {
3513 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3514 &node
->u
._typedef
.type_declarators
);
3517 "cannot add typedef in \"stream\" declaration");
3521 case NODE_TYPEALIAS
:
3522 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3523 node
->u
.typealias
.alias
);
3525 _PERROR("%s", "cannot add typealias in \"stream\" declaration");
3529 case NODE_CTF_EXPRESSION
:
3531 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3537 if (!strcmp(left
, "id")) {
3541 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3542 _PERROR_DUP_ATTR("id", "stream declaration");
3547 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3549 if (ret
|| id
< 0) {
3550 _PERROR("%s", "unexpected unary expression for stream declaration's \"id\" attribute");
3555 ptr
= g_hash_table_lookup(ctx
->stream_classes
,
3558 _PERROR("duplicate stream with ID %" PRId64
,
3564 ret
= bt_ctf_stream_class_set_id(stream_class
, id
);
3567 "cannot set stream declaration's ID");
3571 _SET(set
, _STREAM_ID_SET
);
3572 } else if (!strcmp(left
, "event.header")) {
3573 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3574 _PERROR("%s", "duplicate \"event.header\" entry in stream declaration");
3579 ret
= visit_type_specifier_list(ctx
,
3580 _BT_LIST_FIRST_ENTRY(
3581 &node
->u
.ctf_expression
.right
,
3582 struct ctf_node
, siblings
),
3585 _PERROR("%s", "cannot create event header declaration");
3591 ret
= bt_ctf_stream_class_set_event_header_type(
3592 stream_class
, decl
);
3595 _PERROR("%s", "cannot set stream's event header declaration");
3599 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3600 } else if (!strcmp(left
, "event.context")) {
3601 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3602 _PERROR("%s", "duplicate \"event.context\" entry in stream declaration");
3607 ret
= visit_type_specifier_list(ctx
,
3608 _BT_LIST_FIRST_ENTRY(
3609 &node
->u
.ctf_expression
.right
,
3610 struct ctf_node
, siblings
),
3613 _PERROR("%s", "cannot create stream event context declaration");
3619 ret
= bt_ctf_stream_class_set_event_context_type(
3620 stream_class
, decl
);
3623 _PERROR("%s", "cannot set stream's event context declaration");
3627 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3628 } else if (!strcmp(left
, "packet.context")) {
3629 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3630 _PERROR("%s", "duplicate \"packet.context\" entry in stream declaration");
3635 ret
= visit_type_specifier_list(ctx
,
3636 _BT_LIST_FIRST_ENTRY(
3637 &node
->u
.ctf_expression
.right
,
3638 struct ctf_node
, siblings
),
3641 _PERROR("%s", "cannot create packet context declaration");
3647 ret
= bt_ctf_stream_class_set_packet_context_type(
3648 stream_class
, decl
);
3651 _PERROR("%s", "cannot set stream's packet context declaration");
3655 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3657 _PWARNING("unknown attribute \"%s\" in stream declaration",
3681 int visit_stream_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3686 struct ctf_node
*iter
;
3687 struct bt_ctf_stream_class
*stream_class
= NULL
;
3688 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3690 if (node
->visited
) {
3694 node
->visited
= TRUE
;
3695 stream_class
= create_reset_stream_class(ctx
);
3696 if (!stream_class
) {
3701 ret
= ctx_push_scope(ctx
);
3703 _PERROR("%s", "cannot push scope");
3707 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3708 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3717 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3718 /* Check that packet header has stream_id field */
3719 _BT_CTF_FIELD_TYPE_INIT(stream_id_decl
);
3720 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3722 packet_header_decl
=
3723 bt_ctf_trace_get_packet_header_type(ctx
->trace
);
3724 if (!packet_header_decl
) {
3726 "cannot get trace packet header declaration");
3731 bt_ctf_field_type_structure_get_field_type_by_name(
3732 packet_header_decl
, "stream_id");
3733 BT_PUT(packet_header_decl
);
3734 if (!stream_id_decl
) {
3735 _PERROR("%s", "missing \"stream_id\" field in packet header declaration, but \"id\" attribute is declared for stream");
3739 if (!bt_ctf_field_type_is_integer(stream_id_decl
)) {
3740 BT_PUT(stream_id_decl
);
3741 _PERROR("%s", "\"stream_id\" field in packet header declaration is not an integer");
3745 BT_PUT(stream_id_decl
);
3747 /* Allow only _one_ ID-less stream */
3748 if (g_hash_table_size(ctx
->stream_classes
) != 0) {
3750 "missing \"id\" field in stream declaration");
3755 /* Automatic ID: 0 */
3756 ret
= bt_ctf_stream_class_set_id(stream_class
, 0);
3759 id
= bt_ctf_stream_class_get_id(stream_class
);
3761 _PERROR("wrong stream ID: %" PRId64
, id
);
3766 /* Move reference to visitor's context */
3767 g_hash_table_insert(ctx
->stream_classes
, (gpointer
) (int64_t) id
,
3769 stream_class
= NULL
;
3775 BT_PUT(stream_class
);
3781 int visit_trace_decl_entry(struct ctx
*ctx
, struct ctf_node
*node
, int *set
)
3785 _BT_CTF_FIELD_TYPE_INIT(packet_header_decl
);
3787 switch (node
->type
) {
3789 ret
= visit_typedef(ctx
, node
->u
._typedef
.type_specifier_list
,
3790 &node
->u
._typedef
.type_declarators
);
3793 "cannot add typedef in \"trace\" declaration");
3797 case NODE_TYPEALIAS
:
3798 ret
= visit_typealias(ctx
, node
->u
.typealias
.target
,
3799 node
->u
.typealias
.alias
);
3802 "cannot add typealias in \"trace\" declaration");
3806 case NODE_CTF_EXPRESSION
:
3808 left
= concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3814 if (!strcmp(left
, "major")) {
3815 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3816 _PERROR_DUP_ATTR("major", "trace declaration");
3821 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3824 _PERROR("%s", "unexpected unary expression for trace's \"major\" attribute");
3829 _SET(set
, _TRACE_MAJOR_SET
);
3830 } else if (!strcmp(left
, "minor")) {
3831 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3832 _PERROR_DUP_ATTR("minor", "trace declaration");
3837 ret
= get_unary_unsigned(&node
->u
.ctf_expression
.right
,
3840 _PERROR("%s", "unexpected unary expression for trace's \"minor\" attribute");
3845 _SET(set
, _TRACE_MINOR_SET
);
3846 } else if (!strcmp(left
, "uuid")) {
3847 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3848 _PERROR_DUP_ATTR("uuid", "trace declaration");
3853 ret
= get_unary_uuid(&node
->u
.ctf_expression
.right
,
3857 "invalid trace declaration's UUID");
3861 _SET(set
, _TRACE_UUID_SET
);
3862 } else if (!strcmp(left
, "byte_order")) {
3863 /* Native byte order is already known at this stage */
3864 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3865 _PERROR_DUP_ATTR("byte_order",
3866 "trace declaration");
3871 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3872 } else if (!strcmp(left
, "packet.header")) {
3873 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3874 _PERROR("%s", "duplicate \"packet.header\" entry in trace declaration");
3879 ret
= visit_type_specifier_list(ctx
,
3880 _BT_LIST_FIRST_ENTRY(
3881 &node
->u
.ctf_expression
.right
,
3882 struct ctf_node
, siblings
),
3883 &packet_header_decl
);
3885 _PERROR("%s", "cannot create packet header declaration");
3889 assert(packet_header_decl
);
3890 ret
= bt_ctf_trace_set_packet_header_type(ctx
->trace
,
3891 packet_header_decl
);
3892 BT_PUT(packet_header_decl
);
3894 _PERROR("%s", "cannot set trace declaration's packet header declaration");
3898 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3900 _PWARNING("%s", "unknown attribute \"%s\" in trace declaration");
3908 _PERROR("%s", "unknown expression in trace declaration");
3917 BT_PUT(packet_header_decl
);
3923 int visit_trace_decl(struct ctx
*ctx
, struct ctf_node
*node
)
3927 struct ctf_node
*iter
;
3928 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3930 if (node
->visited
) {
3934 node
->visited
= TRUE
;
3936 if (ctx
->is_trace_visited
) {
3937 _PERROR("%s", "duplicate \"trace\" block");
3942 ret
= ctx_push_scope(ctx
);
3944 _PERROR("%s", "cannot push scope");
3948 bt_list_for_each_entry(iter
, decl_list
, siblings
) {
3949 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3958 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3960 "missing \"major\" attribute in trace declaration");
3965 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3967 "missing \"minor\" attribute in trace declaration");
3972 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3973 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
3978 ctx
->is_trace_visited
= TRUE
;
3988 int visit_env(struct ctx
*ctx
, struct ctf_node
*node
)
3992 struct ctf_node
*entry_node
;
3993 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3995 if (node
->visited
) {
3999 node
->visited
= TRUE
;
4001 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4002 struct bt_list_head
*right_head
=
4003 &entry_node
->u
.ctf_expression
.right
;
4005 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4006 _PERROR("%s", "wrong expression in environment entry");
4011 left
= concatenate_unary_strings(
4012 &entry_node
->u
.ctf_expression
.left
);
4014 _PERROR("%s", "cannot get environment entry name");
4019 if (is_unary_string(right_head
)) {
4020 char *right
= concatenate_unary_strings(right_head
);
4023 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4029 printf_verbose("env.%s = \"%s\"\n", left
, right
);
4030 ret
= bt_ctf_trace_set_environment_field_string(
4031 ctx
->trace
, left
, right
);
4035 _PERROR("environment: cannot add entry \"%s\" to trace",
4039 } else if (is_unary_unsigned(right_head
) ||
4040 is_unary_signed(right_head
)) {
4043 if (is_unary_unsigned(right_head
)) {
4044 ret
= get_unary_unsigned(right_head
,
4047 ret
= get_unary_signed(right_head
, &v
);
4050 _PERROR("unexpected unary expression for environment entry's value (\"%s\")",
4056 printf_verbose("env.%s = %" PRId64
"\n", left
, v
);
4057 ret
= bt_ctf_trace_set_environment_field_integer(
4058 ctx
->trace
, left
, v
);
4060 _PERROR("environment: cannot add entry \"%s\" to trace",
4065 printf_verbose("%s: environment entry \"%s\" has unknown type\n",
4083 int set_trace_byte_order(struct ctx
*ctx
, struct ctf_node
*trace_node
)
4088 struct ctf_node
*node
;
4089 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
4091 bt_list_for_each_entry(node
, decl_list
, siblings
) {
4092 if (node
->type
== NODE_CTF_EXPRESSION
) {
4093 struct ctf_node
*right_node
;
4095 left
= concatenate_unary_strings(
4096 &node
->u
.ctf_expression
.left
);
4102 if (!strcmp(left
, "byte_order")) {
4103 enum bt_ctf_byte_order bo
;
4105 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4106 _PERROR_DUP_ATTR("byte_order",
4107 "trace declaration");
4112 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
4113 right_node
= _BT_LIST_FIRST_ENTRY(
4114 &node
->u
.ctf_expression
.right
,
4115 struct ctf_node
, siblings
);
4116 bo
= byte_order_from_unary_expr(ctx
->efd
,
4118 if (bo
== BT_CTF_BYTE_ORDER_UNKNOWN
) {
4119 _PERROR("%s", "unknown \"byte_order\" attribute in trace declaration");
4122 } else if (bo
== BT_CTF_BYTE_ORDER_NATIVE
) {
4123 _PERROR("%s", "\"byte_order\" attribute cannot be set to \"native\" in trace declaration");
4128 ret
= bt_ctf_trace_set_byte_order(
4131 _PERROR("cannot set trace's byte order (%d)",
4142 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
4143 _PERROR("%s", "missing \"byte_order\" attribute in trace declaration");
4157 int visit_clock_decl_entry(struct ctx
*ctx
, struct ctf_node
*entry_node
,
4158 struct bt_ctf_clock
*clock
, int *set
)
4163 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4168 left
= concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4174 if (!strcmp(left
, "name")) {
4177 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4178 _PERROR_DUP_ATTR("name", "clock declaration");
4183 right
= concatenate_unary_strings(
4184 &entry_node
->u
.ctf_expression
.right
);
4186 _PERROR("%s", "unexpected unary expression for clock declaration's \"name\" attribute");
4191 ret
= bt_ctf_clock_set_name(clock
, right
);
4193 _PERROR("%s", "cannot set clock's name");
4199 _SET(set
, _CLOCK_NAME_SET
);
4200 } else if (!strcmp(left
, "uuid")) {
4201 unsigned char uuid
[BABELTRACE_UUID_LEN
];
4203 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4204 _PERROR_DUP_ATTR("uuid", "clock declaration");
4209 ret
= get_unary_uuid(&entry_node
->u
.ctf_expression
.right
, uuid
);
4211 _PERROR("%s", "invalid clock UUID");
4215 ret
= bt_ctf_clock_set_uuid(clock
, uuid
);
4217 _PERROR("%s", "cannot set clock's UUID");
4221 _SET(set
, _CLOCK_UUID_SET
);
4222 } else if (!strcmp(left
, "description")) {
4225 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4226 _PERROR_DUP_ATTR("description", "clock declaration");
4231 right
= concatenate_unary_strings(
4232 &entry_node
->u
.ctf_expression
.right
);
4234 _PERROR("%s", "unexpected unary expression for clock's \"description\" attribute");
4239 ret
= bt_ctf_clock_set_description(clock
, right
);
4241 _PERROR("%s", "cannot set clock's description");
4247 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4248 } else if (!strcmp(left
, "freq")) {
4251 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4252 _PERROR_DUP_ATTR("freq", "clock declaration");
4257 ret
= get_unary_unsigned(
4258 &entry_node
->u
.ctf_expression
.right
, &freq
);
4260 _PERROR("%s", "unexpected unary expression for clock declaration's \"freq\" attribute");
4265 ret
= bt_ctf_clock_set_frequency(clock
, freq
);
4267 _PERROR("%s", "cannot set clock's frequency");
4271 _SET(set
, _CLOCK_FREQ_SET
);
4272 } else if (!strcmp(left
, "precision")) {
4275 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4276 _PERROR_DUP_ATTR("precision", "clock declaration");
4281 ret
= get_unary_unsigned(
4282 &entry_node
->u
.ctf_expression
.right
, &precision
);
4284 _PERROR("%s", "unexpected unary expression for clock declaration's \"precision\" attribute");
4289 ret
= bt_ctf_clock_set_precision(clock
, precision
);
4291 _PERROR("%s", "cannot set clock's precision");
4295 _SET(set
, _CLOCK_PRECISION_SET
);
4296 } else if (!strcmp(left
, "offset_s")) {
4299 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4300 _PERROR_DUP_ATTR("offset_s", "clock declaration");
4305 ret
= get_unary_unsigned(
4306 &entry_node
->u
.ctf_expression
.right
, &offset_s
);
4308 _PERROR("%s", "unexpected unary expression for clock declaration's \"offset_s\" attribute");
4313 ret
= bt_ctf_clock_set_offset_s(clock
, offset_s
);
4315 _PERROR("%s", "cannot set clock's offset in seconds");
4319 _SET(set
, _CLOCK_OFFSET_S_SET
);
4320 } else if (!strcmp(left
, "offset")) {
4323 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4324 _PERROR_DUP_ATTR("offset", "clock declaration");
4329 ret
= get_unary_unsigned(
4330 &entry_node
->u
.ctf_expression
.right
, &offset
);
4332 _PERROR("%s", "unexpected unary expression for clock declaration's \"offset\" attribute");
4337 ret
= bt_ctf_clock_set_offset(clock
, offset
);
4339 _PERROR("%s", "cannot set clock's offset in cycles");
4343 _SET(set
, _CLOCK_OFFSET_SET
);
4344 } else if (!strcmp(left
, "absolute")) {
4345 struct ctf_node
*right
;
4347 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4348 _PERROR_DUP_ATTR("absolute", "clock declaration");
4353 right
= _BT_LIST_FIRST_ENTRY(
4354 &entry_node
->u
.ctf_expression
.right
,
4355 struct ctf_node
, siblings
);
4356 ret
= get_boolean(ctx
->efd
, right
);
4358 _PERROR("%s", "unexpected unary expression for clock declaration's \"absolute\" attribute");
4363 ret
= bt_ctf_clock_set_is_absolute(clock
, ret
);
4365 _PERROR("%s", "cannot set clock's absolute option");
4369 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4371 _PWARNING("unknown attribute \"%s\" in clock declaration",
4387 int visit_clock_decl(struct ctx
*ctx
, struct ctf_node
*clock_node
)
4391 struct bt_ctf_clock
*clock
;
4392 struct ctf_node
*entry_node
;
4393 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4395 if (clock_node
->visited
) {
4399 clock_node
->visited
= TRUE
;
4400 clock
= bt_ctf_clock_create(NULL
);
4402 _PERROR("%s", "cannot create clock");
4407 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
4408 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
);
4414 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4416 "missing \"name\" attribute in clock declaration");
4421 if (bt_ctf_trace_get_clock_count(ctx
->trace
) != 0) {
4422 _PERROR("%s", "only CTF traces with a single clock declaration are supported as of this version");
4427 ret
= bt_ctf_trace_add_clock(ctx
->trace
, clock
);
4429 _PERROR("%s", "cannot add clock to trace");
4440 int visit_root_decl(struct ctx
*ctx
, struct ctf_node
*root_decl_node
)
4444 if (root_decl_node
->visited
) {
4448 root_decl_node
->visited
= TRUE
;
4450 switch (root_decl_node
->type
) {
4452 ret
= visit_typedef(ctx
,
4453 root_decl_node
->u
._typedef
.type_specifier_list
,
4454 &root_decl_node
->u
._typedef
.type_declarators
);
4456 _PERROR("%s", "cannot add typedef in root scope");
4460 case NODE_TYPEALIAS
:
4461 ret
= visit_typealias(ctx
, root_decl_node
->u
.typealias
.target
,
4462 root_decl_node
->u
.typealias
.alias
);
4464 _PERROR("%s", "cannot add typealias in root scope");
4468 case NODE_TYPE_SPECIFIER_LIST
:
4470 _BT_CTF_FIELD_TYPE_INIT(decl
);
4473 * Just add the type specifier to the root
4474 * declaration scope. Put local reference.
4476 ret
= visit_type_specifier_list(ctx
, root_decl_node
, &decl
);
4495 int add_stream_classes_to_trace(struct ctx
*ctx
)
4498 GHashTableIter iter
;
4499 gpointer key
, stream_class
;
4501 g_hash_table_iter_init(&iter
, ctx
->stream_classes
);
4503 while (g_hash_table_iter_next(&iter
, &key
, &stream_class
)) {
4504 ret
= bt_ctf_trace_add_stream_class(ctx
->trace
,
4507 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
4508 _PERROR("cannot add stream class %" PRId64
" to trace",
4518 int ctf_visitor_generate_ir(FILE *efd
, struct ctf_node
*node
,
4519 struct bt_ctf_trace
**trace
)
4522 struct ctx
*ctx
= NULL
;
4524 printf_verbose("CTF visitor: AST -> CTF IR...\n");
4526 *trace
= bt_ctf_trace_create();
4528 _FPERROR(efd
, "%s", "cannot create trace");
4533 /* Set packet header to NULL to override the default one */
4534 ret
= bt_ctf_trace_set_packet_header_type(*trace
, NULL
);
4538 "cannot set initial, empty packet header structure");
4542 ctx
= ctx_create(*trace
, efd
);
4544 _FPERROR(efd
, "%s", "cannot create visitor context");
4549 switch (node
->type
) {
4552 struct ctf_node
*iter
;
4553 int got_trace_decl
= FALSE
;
4554 int found_callsite
= FALSE
;
4557 * Find trace declaration's byte order first (for early
4560 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4561 if (got_trace_decl
) {
4562 _PERROR("%s", "duplicate trace declaration");
4566 ret
= set_trace_byte_order(ctx
, iter
);
4568 _PERROR("cannot set trace's byte order (%d)",
4573 got_trace_decl
= TRUE
;
4576 if (!got_trace_decl
) {
4577 _PERROR("no trace declaration found (%d)", ret
);
4583 * Visit clocks first since any early integer can be mapped
4586 bt_list_for_each_entry(iter
, &node
->u
.root
.clock
, siblings
) {
4587 ret
= visit_clock_decl(ctx
, iter
);
4589 _PERROR("error while visiting clock declaration (%d)",
4596 * Visit root declarations next, as they can be used by any
4599 bt_list_for_each_entry(iter
, &node
->u
.root
.declaration_list
,
4601 ret
= visit_root_decl(ctx
, iter
);
4603 _PERROR("error while visiting root declaration (%d)",
4609 /* Callsite are not supported */
4610 bt_list_for_each_entry(iter
, &node
->u
.root
.callsite
, siblings
) {
4611 found_callsite
= TRUE
;
4615 if (found_callsite
) {
4616 _PWARNING("%s", "\"callsite\" blocks are not supported as of this version");
4620 bt_list_for_each_entry(iter
, &node
->u
.root
.env
, siblings
) {
4621 ret
= visit_env(ctx
, iter
);
4623 _PERROR("error while visiting environment block (%d)",
4630 bt_list_for_each_entry(iter
, &node
->u
.root
.trace
, siblings
) {
4631 ret
= visit_trace_decl(ctx
, iter
);
4633 _PERROR("%s", "error while visiting trace declaration");
4639 bt_list_for_each_entry(iter
, &node
->u
.root
.stream
, siblings
) {
4640 ret
= visit_stream_decl(ctx
, iter
);
4642 _PERROR("%s", "error while visiting stream declaration");
4648 bt_list_for_each_entry(iter
, &node
->u
.root
.event
, siblings
) {
4649 ret
= visit_event_decl(ctx
, iter
);
4651 _PERROR("%s", "error while visiting event declaration");
4659 _PERROR("unknown node type: %d", (int) node
->type
);
4664 /* Add stream classes to trace now */
4665 ret
= add_stream_classes_to_trace(ctx
);
4667 _PERROR("%s", "cannot add stream classes to trace");
4671 printf_verbose("done!\n");