2 * SPDX-License-Identifier: MIT
4 * Copyright 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2015-2018 Philippe Proulx <philippe.proulx@efficios.com>
7 * Common Trace Format metadata visitor (generates CTF IR objects).
13 #include "logging.hpp"
15 #include "common/assert.h"
16 #include "common/common.h"
17 #include "common/uuid.h"
18 #include "compat/endian.h" /* IWYU pragma: keep */
19 #include "cpp-common/bt2c/logging.hpp"
22 #include "ctf-meta-visitors.hpp"
24 /* Bit value (left shift) */
25 #define _BV(_val) (1 << (_val))
27 /* Bit is set in a set of bits */
28 #define _IS_SET(_set, _mask) (*(_set) & (_mask))
30 /* Set bit in a set of bits */
31 #define _SET(_set, _mask) (*(_set) |= (_mask))
33 /* Try to push scope, or go to the `error` label */
34 #define _TRY_PUSH_SCOPE_OR_GOTO_ERROR() \
36 ret = ctx_push_scope(ctx); \
38 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx->logger, "Cannot push scope."); \
43 /* Bits for verifying existing attributes in various declarations */
46 _CLOCK_NAME_SET
= _BV(0),
47 _CLOCK_UUID_SET
= _BV(1),
48 _CLOCK_FREQ_SET
= _BV(2),
49 _CLOCK_PRECISION_SET
= _BV(3),
50 _CLOCK_OFFSET_S_SET
= _BV(4),
51 _CLOCK_OFFSET_SET
= _BV(5),
52 _CLOCK_ABSOLUTE_SET
= _BV(6),
53 _CLOCK_DESCRIPTION_SET
= _BV(7),
58 _INTEGER_ALIGN_SET
= _BV(0),
59 _INTEGER_SIZE_SET
= _BV(1),
60 _INTEGER_BASE_SET
= _BV(2),
61 _INTEGER_ENCODING_SET
= _BV(3),
62 _INTEGER_BYTE_ORDER_SET
= _BV(4),
63 _INTEGER_SIGNED_SET
= _BV(5),
64 _INTEGER_MAP_SET
= _BV(6),
69 _FLOAT_ALIGN_SET
= _BV(0),
70 _FLOAT_MANT_DIG_SET
= _BV(1),
71 _FLOAT_EXP_DIG_SET
= _BV(2),
72 _FLOAT_BYTE_ORDER_SET
= _BV(3),
77 _STRING_ENCODING_SET
= _BV(0),
82 _TRACE_MINOR_SET
= _BV(0),
83 _TRACE_MAJOR_SET
= _BV(1),
84 _TRACE_BYTE_ORDER_SET
= _BV(2),
85 _TRACE_UUID_SET
= _BV(3),
86 _TRACE_PACKET_HEADER_SET
= _BV(4),
91 _STREAM_ID_SET
= _BV(0),
92 _STREAM_PACKET_CONTEXT_SET
= _BV(1),
93 _STREAM_EVENT_HEADER_SET
= _BV(2),
94 _STREAM_EVENT_CONTEXT_SET
= _BV(3),
99 _EVENT_NAME_SET
= _BV(0),
100 _EVENT_ID_SET
= _BV(1),
101 _EVENT_MODEL_EMF_URI_SET
= _BV(2),
102 _EVENT_STREAM_ID_SET
= _BV(3),
103 _EVENT_LOG_LEVEL_SET
= _BV(4),
104 _EVENT_CONTEXT_SET
= _BV(5),
105 _EVENT_FIELDS_SET
= _BV(6),
114 LOG_LEVEL_WARNING
= 4,
115 LOG_LEVEL_NOTICE
= 5,
117 LOG_LEVEL_DEBUG_SYSTEM
= 7,
118 LOG_LEVEL_DEBUG_PROGRAM
= 8,
119 LOG_LEVEL_DEBUG_PROCESS
= 9,
120 LOG_LEVEL_DEBUG_MODULE
= 10,
121 LOG_LEVEL_DEBUG_UNIT
= 11,
122 LOG_LEVEL_DEBUG_FUNCTION
= 12,
123 LOG_LEVEL_DEBUG_LINE
= 13,
124 LOG_LEVEL_DEBUG
= 14,
128 /* Prefixes of class aliases */
129 #define _PREFIX_ALIAS 'a'
130 #define _PREFIX_ENUM 'e'
131 #define _PREFIX_STRUCT 's'
132 #define _PREFIX_VARIANT 'v'
134 /* First entry in a BT list */
135 #define _BT_LIST_FIRST_ENTRY(_ptr, _class, _member) bt_list_entry((_ptr)->next, _class, _member)
137 #define _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(_node, _attr, _entity) \
138 _BT_CPPLOGE_APPEND_CAUSE_LINENO(ctx->logger, (_node)->lineno, \
139 "Duplicate attribute in {}: attr-name=\"{}\"", _entity, _attr)
141 #define _BT_CPPLOGE_NODE(_node, _msg, args...) \
142 _BT_CPPLOGE_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
144 #define _BT_CPPLOGE_APPEND_CAUSE_NODE(_node, _msg, args...) \
145 _BT_CPPLOGE_APPEND_CAUSE_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
147 #define _BT_CPPLOGW_NODE(_node, _msg, args...) \
148 _BT_CPPLOGW_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
150 #define _BT_CPPLOGT_NODE(_node, _msg, args...) \
151 _BT_CPPLOGT_LINENO(ctx->logger, (_node)->lineno, _msg, ##args)
154 * Declaration scope of a visitor context. This represents a TSDL
155 * lexical scope, so that aliases and named structures, variants,
156 * and enumerations may be registered and looked up hierarchically.
158 struct ctx_decl_scope
161 * Alias name to field class.
163 * GQuark -> struct ctf_field_class * (owned by this)
165 GHashTable
*decl_map
;
167 /* Parent scope; NULL if this is the root declaration scope */
168 struct ctx_decl_scope
*parent_scope
;
172 * Creates a new declaration scope.
174 * @param par_scope Parent scope (NULL if creating a root scope)
175 * @returns New declaration scope, or NULL on error
177 static struct ctx_decl_scope
*ctx_decl_scope_create(struct ctf_visitor_generate_ir
*ctx
,
178 struct ctx_decl_scope
*par_scope
)
180 struct ctx_decl_scope
*scope
;
182 scope
= g_new(struct ctx_decl_scope
, 1);
184 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Failed to allocate one declaration scope.");
188 scope
->decl_map
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
189 (GDestroyNotify
) ctf_field_class_destroy
);
190 scope
->parent_scope
= par_scope
;
197 * Destroys a declaration scope.
199 * This function does not destroy the parent scope.
201 * @param scope Scope to destroy
203 static void ctx_decl_scope_destroy(struct ctx_decl_scope
*scope
)
209 g_hash_table_destroy(scope
->decl_map
);
217 * Returns the GQuark of a prefixed alias.
219 * @param prefix Prefix character
221 * @returns Associated GQuark, or 0 on error
223 static GQuark
get_prefixed_named_quark(char prefix
, const char *name
)
226 std::string prname
= std::string
{prefix
} + name
;
227 return g_quark_from_string(prname
.c_str());
231 * Looks up a prefixed class alias within a declaration scope.
233 * @param scope Declaration scope
234 * @param prefix Prefix character
235 * @param name Alias name
236 * @param levels Number of levels to dig into (-1 means infinite)
237 * @param copy True to return a copy
238 * @returns Declaration (owned by caller if \p copy is true),
239 * or NULL if not found
241 static struct ctf_field_class
*ctx_decl_scope_lookup_prefix_alias(struct ctx_decl_scope
*scope
,
242 char prefix
, const char *name
,
243 int levels
, bool copy
)
247 struct ctf_field_class
*decl
= NULL
;
248 struct ctx_decl_scope
*cur_scope
= scope
;
252 qname
= get_prefixed_named_quark(prefix
, name
);
261 while (cur_scope
&& cur_levels
< levels
) {
262 decl
= (ctf_field_class
*) g_hash_table_lookup(cur_scope
->decl_map
,
263 (gconstpointer
) GUINT_TO_POINTER(qname
));
265 /* Caller's reference */
267 decl
= ctf_field_class_copy(decl
);
274 cur_scope
= cur_scope
->parent_scope
;
283 * Looks up a class alias within a declaration scope.
285 * @param scope Declaration scope
286 * @param name Alias name
287 * @param levels Number of levels to dig into (-1 means infinite)
288 * @param copy True to return a copy
289 * @returns Declaration (owned by caller if \p copy is true),
290 * or NULL if not found
292 static struct ctf_field_class
*ctx_decl_scope_lookup_alias(struct ctx_decl_scope
*scope
,
293 const char *name
, int levels
, bool copy
)
295 return ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ALIAS
, name
, levels
, copy
);
299 * Looks up an enumeration within a declaration scope.
301 * @param scope Declaration scope
302 * @param name Enumeration name
303 * @param levels Number of levels to dig into (-1 means infinite)
304 * @param copy True to return a copy
305 * @returns Declaration (owned by caller if \p copy is true),
306 * or NULL if not found
308 static struct ctf_field_class_enum
*
309 ctx_decl_scope_lookup_enum(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
311 return ctf_field_class_as_enum(
312 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_ENUM
, name
, levels
, copy
));
316 * Looks up a structure within a declaration scope.
318 * @param scope Declaration scope
319 * @param name Structure name
320 * @param levels Number of levels to dig into (-1 means infinite)
321 * @param copy True to return a copy
322 * @returns Declaration (owned by caller if \p copy is true),
323 * or NULL if not found
325 static struct ctf_field_class_struct
*
326 ctx_decl_scope_lookup_struct(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
328 return ctf_field_class_as_struct(
329 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_STRUCT
, name
, levels
, copy
));
333 * Looks up a variant within a declaration scope.
335 * @param scope Declaration scope
336 * @param name Variant name
337 * @param levels Number of levels to dig into (-1 means infinite)
338 * @param copy True to return a copy
339 * @returns Declaration (owned by caller if \p copy is true),
340 * or NULL if not found
342 static struct ctf_field_class_variant
*
343 ctx_decl_scope_lookup_variant(struct ctx_decl_scope
*scope
, const char *name
, int levels
, bool copy
)
345 return ctf_field_class_as_variant(
346 ctx_decl_scope_lookup_prefix_alias(scope
, _PREFIX_VARIANT
, name
, levels
, copy
));
350 * Registers a prefixed class alias within a declaration scope.
352 * @param scope Declaration scope
353 * @param prefix Prefix character
354 * @param name Alias name (non-NULL)
355 * @param decl Field class to register (copied)
356 * @returns 0 if registration went okay, negative value otherwise
358 static int ctx_decl_scope_register_prefix_alias(struct ctx_decl_scope
*scope
, char prefix
,
359 const char *name
, struct ctf_field_class
*decl
)
367 qname
= get_prefixed_named_quark(prefix
, name
);
373 /* Make sure alias does not exist in local scope */
374 if (ctx_decl_scope_lookup_prefix_alias(scope
, prefix
, name
, 1, false)) {
379 decl
= ctf_field_class_copy(decl
);
381 g_hash_table_insert(scope
->decl_map
, GUINT_TO_POINTER(qname
), decl
);
388 * Registers a class alias within a declaration scope.
390 * @param scope Declaration scope
391 * @param name Alias name (non-NULL)
392 * @param decl Field class to register (copied)
393 * @returns 0 if registration went okay, negative value otherwise
395 static int ctx_decl_scope_register_alias(struct ctx_decl_scope
*scope
, const char *name
,
396 struct ctf_field_class
*decl
)
398 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ALIAS
, name
, decl
);
402 * Registers an enumeration declaration within a declaration scope.
404 * @param scope Declaration scope
405 * @param name Enumeration name (non-NULL)
406 * @param decl Enumeration field class to register (copied)
407 * @returns 0 if registration went okay, negative value otherwise
409 static int ctx_decl_scope_register_enum(struct ctx_decl_scope
*scope
, const char *name
,
410 struct ctf_field_class_enum
*decl
)
412 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_ENUM
, name
, &decl
->base
.base
.base
);
416 * Registers a structure declaration within a declaration scope.
418 * @param scope Declaration scope
419 * @param name Structure name (non-NULL)
420 * @param decl Structure field class to register (copied)
421 * @returns 0 if registration went okay, negative value otherwise
423 static int ctx_decl_scope_register_struct(struct ctx_decl_scope
*scope
, const char *name
,
424 struct ctf_field_class_struct
*decl
)
426 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_STRUCT
, name
, &decl
->base
);
430 * Registers a variant declaration within a declaration scope.
432 * @param scope Declaration scope
433 * @param name Variant name (non-NULL)
434 * @param decl Variant field class to register
435 * @returns 0 if registration went okay, negative value otherwise
437 static int ctx_decl_scope_register_variant(struct ctx_decl_scope
*scope
, const char *name
,
438 struct ctf_field_class_variant
*decl
)
440 return ctx_decl_scope_register_prefix_alias(scope
, _PREFIX_VARIANT
, name
, &decl
->base
);
444 * Destroys a visitor context.
446 * @param ctx Visitor context to destroy
448 static void ctx_destroy(struct ctf_visitor_generate_ir
*ctx
)
450 struct ctx_decl_scope
*scope
;
456 scope
= ctx
->current_scope
;
459 * Destroy all scopes, from current one to the root scope.
462 struct ctx_decl_scope
*parent_scope
= scope
->parent_scope
;
464 ctx_decl_scope_destroy(scope
);
465 scope
= parent_scope
;
468 bt_trace_class_put_ref(ctx
->trace_class
);
471 ctf_trace_class_destroy(ctx
->ctf_tc
);
481 * Creates a new visitor context.
483 * @param trace Associated trace
484 * @returns New visitor context, or NULL on error
486 static struct ctf_visitor_generate_ir
*
487 ctx_create(const struct ctf_metadata_decoder_config
*decoder_config
, const bt2c::Logger
& logger
)
489 BT_ASSERT(decoder_config
);
491 ctf_visitor_generate_ir
*ctx
= new ctf_visitor_generate_ir
{*decoder_config
, logger
};
493 if (decoder_config
->self_comp
) {
494 ctx
->trace_class
= bt_trace_class_create(decoder_config
->self_comp
);
495 if (!ctx
->trace_class
) {
496 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create empty trace class.");
501 ctx
->ctf_tc
= ctf_trace_class_create();
503 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create CTF trace class.");
507 /* Root declaration scope */
508 ctx
->current_scope
= ctx_decl_scope_create(ctx
, NULL
);
509 if (!ctx
->current_scope
) {
510 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create declaration scope.");
525 * Pushes a new declaration scope on top of a visitor context's
526 * declaration scope stack.
528 * @param ctx Visitor context
529 * @returns 0 on success, or a negative value on error
531 static int ctx_push_scope(struct ctf_visitor_generate_ir
*ctx
)
534 struct ctx_decl_scope
*new_scope
;
537 new_scope
= ctx_decl_scope_create(ctx
, ctx
->current_scope
);
539 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "Cannot create declaration scope.");
544 ctx
->current_scope
= new_scope
;
550 static void ctx_pop_scope(struct ctf_visitor_generate_ir
*ctx
)
552 struct ctx_decl_scope
*parent_scope
= NULL
;
556 if (!ctx
->current_scope
) {
560 parent_scope
= ctx
->current_scope
->parent_scope
;
561 ctx_decl_scope_destroy(ctx
->current_scope
);
562 ctx
->current_scope
= parent_scope
;
568 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
569 struct ctf_node
*ts_list
,
570 struct ctf_field_class
**decl
);
572 static int is_unary_string(struct bt_list_head
*head
)
575 struct ctf_node
*node
;
577 bt_list_for_each_entry (node
, head
, siblings
) {
578 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
582 if (node
->u
.unary_expression
.type
!= UNARY_STRING
) {
590 static const char *get_map_clock_name_value(struct bt_list_head
*head
)
593 struct ctf_node
*node
;
594 const char *name
= NULL
;
596 bt_list_for_each_entry (node
, head
, siblings
) {
598 int uexpr_type
= node
->u
.unary_expression
.type
;
599 int uexpr_link
= node
->u
.unary_expression
.link
;
600 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_STRING
||
601 !((uexpr_link
!= UNARY_LINK_UNKNOWN
) ^ (i
== 0));
606 /* Needs to be chained with . */
607 switch (node
->u
.unary_expression
.link
) {
610 case UNARY_ARROWLINK
:
611 case UNARY_DOTDOTDOT
:
617 src_string
= node
->u
.unary_expression
.u
.string
;
621 if (strcmp("clock", src_string
)) {
629 if (strcmp("value", src_string
)) {
634 /* Extra identifier, unknown */
647 static int is_unary_unsigned(struct bt_list_head
*head
)
650 struct ctf_node
*node
;
652 bt_list_for_each_entry (node
, head
, siblings
) {
653 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
657 if (node
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
665 static int get_unary_unsigned(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
670 struct ctf_node
*node
;
674 if (bt_list_empty(head
)) {
679 bt_list_for_each_entry (node
, head
, siblings
) {
680 int uexpr_type
= node
->u
.unary_expression
.type
;
681 int uexpr_link
= node
->u
.unary_expression
.link
;
682 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
|| uexpr_type
!= UNARY_UNSIGNED_CONSTANT
||
683 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
685 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Invalid constant unsigned integer.");
690 *value
= node
->u
.unary_expression
.u
.unsigned_constant
;
698 static int is_unary_signed(struct bt_list_head
*head
)
701 struct ctf_node
*node
;
703 bt_list_for_each_entry (node
, head
, siblings
) {
704 if (node
->type
!= NODE_UNARY_EXPRESSION
) {
708 if (node
->u
.unary_expression
.type
!= UNARY_SIGNED_CONSTANT
) {
716 static int get_unary_signed(struct bt_list_head
*head
, int64_t *value
)
720 struct ctf_node
*node
;
722 bt_list_for_each_entry (node
, head
, siblings
) {
723 int uexpr_type
= node
->u
.unary_expression
.type
;
724 int uexpr_link
= node
->u
.unary_expression
.link
;
725 int cond
= node
->type
!= NODE_UNARY_EXPRESSION
||
726 (uexpr_type
!= UNARY_UNSIGNED_CONSTANT
&& uexpr_type
!= UNARY_SIGNED_CONSTANT
) ||
727 uexpr_link
!= UNARY_LINK_UNKNOWN
|| i
!= 0;
733 switch (uexpr_type
) {
734 case UNARY_UNSIGNED_CONSTANT
:
735 *value
= (int64_t) node
->u
.unary_expression
.u
.unsigned_constant
;
737 case UNARY_SIGNED_CONSTANT
:
738 *value
= node
->u
.unary_expression
.u
.signed_constant
;
752 static int get_unary_uuid(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*head
,
755 return ctf_ast_get_unary_uuid(head
, uuid
, ctx
->logger
);
758 static int get_boolean(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*unary_expr
)
762 if (unary_expr
->type
!= NODE_UNARY_EXPRESSION
) {
763 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
, "Expecting unary expression: node-type={}",
769 switch (unary_expr
->u
.unary_expression
.type
) {
770 case UNARY_UNSIGNED_CONSTANT
:
771 ret
= (unary_expr
->u
.unary_expression
.u
.unsigned_constant
!= 0);
773 case UNARY_SIGNED_CONSTANT
:
774 ret
= (unary_expr
->u
.unary_expression
.u
.signed_constant
!= 0);
778 const char *str
= unary_expr
->u
.unary_expression
.u
.string
;
780 if (strcmp(str
, "true") == 0 || strcmp(str
, "TRUE") == 0) {
782 } else if (strcmp(str
, "false") == 0 || strcmp(str
, "FALSE") == 0) {
785 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
, "Unexpected boolean value: value=\"{}\"",
793 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
, "Unexpected unary expression type: node-type={}",
794 unary_expr
->u
.unary_expression
.type
);
803 static enum ctf_byte_order
byte_order_from_unary_expr(struct ctf_visitor_generate_ir
*ctx
,
804 struct ctf_node
*unary_expr
)
807 enum ctf_byte_order bo
= CTF_BYTE_ORDER_UNKNOWN
;
809 if (unary_expr
->u
.unary_expression
.type
!= UNARY_STRING
) {
810 _BT_CPPLOGE_APPEND_CAUSE_NODE(
811 unary_expr
, "\"byte_order\" attribute: expecting `be`, `le`, `network`, or `native`.");
815 str
= unary_expr
->u
.unary_expression
.u
.string
;
817 if (strcmp(str
, "be") == 0 || strcmp(str
, "network") == 0) {
818 bo
= CTF_BYTE_ORDER_BIG
;
819 } else if (strcmp(str
, "le") == 0) {
820 bo
= CTF_BYTE_ORDER_LITTLE
;
821 } else if (strcmp(str
, "native") == 0) {
822 bo
= CTF_BYTE_ORDER_DEFAULT
;
824 _BT_CPPLOGE_APPEND_CAUSE_NODE(unary_expr
,
825 "Unexpected \"byte_order\" attribute value: "
826 "expecting `be`, `le`, `network`, or `native`: value=\"{}\"",
835 static enum ctf_byte_order
get_real_byte_order(struct ctf_visitor_generate_ir
*ctx
,
836 struct ctf_node
*uexpr
)
838 enum ctf_byte_order bo
= byte_order_from_unary_expr(ctx
, uexpr
);
840 if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
841 bo
= ctx
->ctf_tc
->default_byte_order
;
847 static int is_align_valid(uint64_t align
)
849 return (align
!= 0) && !(align
& (align
- UINT64_C(1)));
852 static int get_class_specifier_name(struct ctf_visitor_generate_ir
*ctx
,
853 struct ctf_node
*cls_specifier
, GString
*str
)
857 if (cls_specifier
->type
!= NODE_TYPE_SPECIFIER
) {
858 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier
, "Unexpected node type: node-type={}",
859 cls_specifier
->type
);
864 switch (cls_specifier
->u
.field_class_specifier
.type
) {
866 g_string_append(str
, "void");
869 g_string_append(str
, "char");
872 g_string_append(str
, "short");
875 g_string_append(str
, "int");
878 g_string_append(str
, "long");
881 g_string_append(str
, "float");
883 case TYPESPEC_DOUBLE
:
884 g_string_append(str
, "double");
886 case TYPESPEC_SIGNED
:
887 g_string_append(str
, "signed");
889 case TYPESPEC_UNSIGNED
:
890 g_string_append(str
, "unsigned");
893 g_string_append(str
, "bool");
895 case TYPESPEC_COMPLEX
:
896 g_string_append(str
, "_Complex");
898 case TYPESPEC_IMAGINARY
:
899 g_string_append(str
, "_Imaginary");
902 g_string_append(str
, "const");
904 case TYPESPEC_ID_TYPE
:
905 if (cls_specifier
->u
.field_class_specifier
.id_type
) {
906 g_string_append(str
, cls_specifier
->u
.field_class_specifier
.id_type
);
909 case TYPESPEC_STRUCT
:
911 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
913 if (!node
->u
._struct
.name
) {
914 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty structure field class name.");
919 g_string_append(str
, "struct ");
920 g_string_append(str
, node
->u
._struct
.name
);
923 case TYPESPEC_VARIANT
:
925 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
927 if (!node
->u
.variant
.name
) {
928 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unexpected empty variant field class name.");
933 g_string_append(str
, "variant ");
934 g_string_append(str
, node
->u
.variant
.name
);
939 struct ctf_node
*node
= cls_specifier
->u
.field_class_specifier
.node
;
941 if (!node
->u
._enum
.enum_id
) {
942 _BT_CPPLOGE_APPEND_CAUSE_NODE(
943 node
, "Unexpected empty enumeration field class (`enum`) name.");
948 g_string_append(str
, "enum ");
949 g_string_append(str
, node
->u
._enum
.enum_id
);
952 case TYPESPEC_FLOATING_POINT
:
953 case TYPESPEC_INTEGER
:
954 case TYPESPEC_STRING
:
956 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier
->u
.field_class_specifier
.node
,
957 "Unexpected field class specifier type: {}",
958 cls_specifier
->u
.field_class_specifier
.type
);
967 static int get_class_specifier_list_name(struct ctf_visitor_generate_ir
*ctx
,
968 struct ctf_node
*cls_specifier_list
, GString
*str
)
971 struct ctf_node
*iter
;
972 int alias_item_nr
= 0;
973 struct bt_list_head
*head
= &cls_specifier_list
->u
.field_class_specifier_list
.head
;
975 bt_list_for_each_entry (iter
, head
, siblings
) {
976 if (alias_item_nr
!= 0) {
977 g_string_append(str
, " ");
981 ret
= get_class_specifier_name(ctx
, iter
, str
);
991 static GQuark
create_class_alias_identifier(struct ctf_visitor_generate_ir
*ctx
,
992 struct ctf_node
*cls_specifier_list
,
993 struct ctf_node
*node_field_class_declarator
)
999 struct ctf_node
*iter
;
1000 struct bt_list_head
*pointers
= &node_field_class_declarator
->u
.field_class_declarator
.pointers
;
1002 str
= g_string_new("");
1003 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
1005 g_string_free(str
, TRUE
);
1009 bt_list_for_each_entry (iter
, pointers
, siblings
) {
1010 g_string_append(str
, " *");
1012 if (iter
->u
.pointer
.const_qualifier
) {
1013 g_string_append(str
, " const");
1017 str_c
= g_string_free(str
, FALSE
);
1018 qalias
= g_quark_from_string(str_c
);
1025 static int visit_field_class_declarator(struct ctf_visitor_generate_ir
*ctx
,
1026 struct ctf_node
*cls_specifier_list
, GQuark
*field_name
,
1027 struct ctf_node
*node_field_class_declarator
,
1028 struct ctf_field_class
**field_decl
,
1029 struct ctf_field_class
*nested_decl
)
1032 * During this whole function, nested_decl is always OURS,
1033 * whereas field_decl is an output which we create, but
1034 * belongs to the caller (it is moved).
1039 /* Validate field class declarator node */
1040 if (node_field_class_declarator
) {
1041 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_UNKNOWN
) {
1042 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1043 node_field_class_declarator
, "Unexpected field class declarator type: type={}",
1044 node_field_class_declarator
->u
.field_class_declarator
.type
);
1049 /* TODO: GCC bitfields not supported yet */
1050 if (node_field_class_declarator
->u
.field_class_declarator
.bitfield_len
!= NULL
) {
1051 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1052 "GCC bitfields are not supported as of this version.");
1058 /* Find the right nested declaration if not provided */
1060 if (node_field_class_declarator
&&
1061 !bt_list_empty(&node_field_class_declarator
->u
.field_class_declarator
.pointers
)) {
1065 * If we have a pointer declarator, it HAS to
1066 * be present in the field class aliases (else
1070 create_class_alias_identifier(ctx
, cls_specifier_list
, node_field_class_declarator
);
1071 nested_decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, g_quark_to_string(qalias
),
1074 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1075 "Cannot find class alias: name=\"{}\"",
1076 g_quark_to_string(qalias
));
1081 if (nested_decl
->type
== CTF_FIELD_CLASS_TYPE_INT
) {
1082 /* Pointer: force integer's base to 16 */
1083 struct ctf_field_class_int
*int_fc
= ctf_field_class_as_int(nested_decl
);
1085 int_fc
->disp_base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
1088 ret
= visit_field_class_specifier_list(ctx
, cls_specifier_list
, &nested_decl
);
1090 BT_ASSERT(!nested_decl
);
1096 BT_ASSERT(nested_decl
);
1098 if (!node_field_class_declarator
) {
1099 *field_decl
= nested_decl
;
1104 if (node_field_class_declarator
->u
.field_class_declarator
.type
== TYPEDEC_ID
) {
1105 if (node_field_class_declarator
->u
.field_class_declarator
.u
.id
) {
1106 const char *id
= node_field_class_declarator
->u
.field_class_declarator
.u
.id
;
1108 *field_name
= g_quark_from_string(id
);
1113 *field_decl
= nested_decl
;
1117 struct ctf_node
*first
;
1118 struct ctf_field_class
*decl
= NULL
;
1119 struct ctf_field_class
*outer_field_decl
= NULL
;
1120 struct bt_list_head
*length
=
1121 &node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.length
;
1123 /* Create array/sequence, pass nested_decl as child */
1124 if (bt_list_empty(length
)) {
1125 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1126 "Expecting length field reference or value.");
1131 first
= _BT_LIST_FIRST_ENTRY(length
, struct ctf_node
, siblings
);
1132 if (first
->type
!= NODE_UNARY_EXPRESSION
) {
1133 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type={}", first
->type
);
1138 switch (first
->u
.unary_expression
.type
) {
1139 case UNARY_UNSIGNED_CONSTANT
:
1141 struct ctf_field_class_array
*array_decl
= NULL
;
1143 array_decl
= ctf_field_class_array_create();
1144 BT_ASSERT(array_decl
);
1145 array_decl
->length
= first
->u
.unary_expression
.u
.unsigned_constant
;
1146 array_decl
->base
.elem_fc
= nested_decl
;
1148 decl
= &array_decl
->base
.base
;
1153 /* Lookup unsigned integer definition, create seq. */
1154 struct ctf_field_class_sequence
*seq_decl
= NULL
;
1155 char *length_name
= ctf_ast_concatenate_unary_strings(length
);
1158 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1159 "Cannot concatenate unary strings.");
1164 if (strncmp(length_name
, "env.", 4) == 0) {
1165 /* This is, in fact, an array */
1166 const char *env_entry_name
= &length_name
[4];
1167 struct ctf_trace_class_env_entry
*env_entry
=
1168 ctf_trace_class_borrow_env_entry_by_name(ctx
->ctf_tc
, env_entry_name
);
1169 struct ctf_field_class_array
*array_decl
;
1172 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1173 "Cannot find environment entry: "
1180 if (env_entry
->type
!= CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
) {
1181 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1182 "Wrong environment entry type "
1183 "(expecting integer): "
1190 if (env_entry
->value
.i
< 0) {
1191 _BT_CPPLOGE_APPEND_CAUSE_NODE(node_field_class_declarator
,
1192 "Invalid, negative array length: "
1193 "env-entry-name=\"{}\", "
1195 env_entry_name
, env_entry
->value
.i
);
1200 array_decl
= ctf_field_class_array_create();
1201 BT_ASSERT(array_decl
);
1202 array_decl
->length
= (uint64_t) env_entry
->value
.i
;
1203 array_decl
->base
.elem_fc
= nested_decl
;
1205 decl
= &array_decl
->base
.base
;
1207 seq_decl
= ctf_field_class_sequence_create();
1208 BT_ASSERT(seq_decl
);
1209 seq_decl
->base
.elem_fc
= nested_decl
;
1211 g_string_assign(seq_decl
->length_ref
, length_name
);
1212 decl
= &seq_decl
->base
.base
;
1215 g_free(length_name
);
1223 BT_ASSERT(!nested_decl
);
1225 BT_ASSERT(!*field_decl
);
1228 * At this point, we found the next nested declaration.
1229 * We currently own this (and lost the ownership of
1230 * nested_decl in the meantime). Pass this next
1231 * nested declaration as the content of the outer
1232 * container, MOVING its ownership.
1234 ret
= visit_field_class_declarator(
1235 ctx
, cls_specifier_list
, field_name
,
1236 node_field_class_declarator
->u
.field_class_declarator
.u
.nested
.field_class_declarator
,
1237 &outer_field_decl
, decl
);
1240 BT_ASSERT(!outer_field_decl
);
1245 BT_ASSERT(outer_field_decl
);
1246 *field_decl
= outer_field_decl
;
1247 outer_field_decl
= NULL
;
1250 BT_ASSERT(*field_decl
);
1254 ctf_field_class_destroy(*field_decl
);
1262 ctf_field_class_destroy(nested_decl
);
1267 static int visit_struct_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1268 struct ctf_field_class_struct
*struct_decl
,
1269 struct ctf_node
*cls_specifier_list
,
1270 struct bt_list_head
*field_class_declarators
)
1273 struct ctf_node
*iter
;
1274 struct ctf_field_class
*field_decl
= NULL
;
1276 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1279 const char *field_name
;
1281 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1284 BT_ASSERT(!field_decl
);
1285 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1286 "Cannot visit field class declarator: ret={}", ret
);
1290 BT_ASSERT(field_decl
);
1291 field_name
= g_quark_to_string(qfield_name
);
1293 /* Check if field with same name already exists */
1294 if (ctf_field_class_struct_borrow_member_by_name(struct_decl
, field_name
)) {
1295 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1296 "Duplicate field in structure field class: "
1297 "field-name=\"{}\"",
1303 /* Add field to structure */
1304 ctf_field_class_struct_append_member(struct_decl
, field_name
, field_decl
);
1311 ctf_field_class_destroy(field_decl
);
1316 static int visit_variant_decl_field(struct ctf_visitor_generate_ir
*ctx
,
1317 struct ctf_field_class_variant
*variant_decl
,
1318 struct ctf_node
*cls_specifier_list
,
1319 struct bt_list_head
*field_class_declarators
)
1322 struct ctf_node
*iter
;
1323 struct ctf_field_class
*field_decl
= NULL
;
1325 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1328 const char *field_name
;
1330 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qfield_name
, iter
, &field_decl
,
1333 BT_ASSERT(!field_decl
);
1334 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1335 "Cannot visit field class declarator: ret={}", ret
);
1339 BT_ASSERT(field_decl
);
1340 field_name
= g_quark_to_string(qfield_name
);
1342 /* Check if field with same name already exists */
1343 if (ctf_field_class_variant_borrow_option_by_name(variant_decl
, field_name
)) {
1344 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
1345 "Duplicate field in variant field class: "
1346 "field-name=\"{}\"",
1352 /* Add field to structure */
1353 ctf_field_class_variant_append_option(variant_decl
, field_name
, field_decl
);
1360 ctf_field_class_destroy(field_decl
);
1365 static int visit_field_class_def(struct ctf_visitor_generate_ir
*ctx
,
1366 struct ctf_node
*cls_specifier_list
,
1367 struct bt_list_head
*field_class_declarators
)
1371 struct ctf_node
*iter
;
1372 struct ctf_field_class
*class_decl
= NULL
;
1374 bt_list_for_each_entry (iter
, field_class_declarators
, siblings
) {
1375 ret
= visit_field_class_declarator(ctx
, cls_specifier_list
, &qidentifier
, iter
, &class_decl
,
1378 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit field class declarator: ret={}", ret
);
1383 /* Do not allow field class def and alias of untagged variants */
1384 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1385 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1387 if (var_fc
->tag_path
.path
->len
== 0) {
1388 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1389 iter
, "Type definition of untagged variant field class is not allowed.");
1395 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
, g_quark_to_string(qidentifier
),
1398 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot register field class alias: name=\"{}\"",
1399 g_quark_to_string(qidentifier
));
1405 ctf_field_class_destroy(class_decl
);
1410 static int visit_field_class_alias(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*target
,
1411 struct ctf_node
*alias
)
1415 struct ctf_node
*node
;
1416 GQuark qdummy_field_name
;
1417 struct ctf_field_class
*class_decl
= NULL
;
1419 /* Create target field class */
1420 if (bt_list_empty(&target
->u
.field_class_alias_target
.field_class_declarators
)) {
1423 node
= _BT_LIST_FIRST_ENTRY(&target
->u
.field_class_alias_target
.field_class_declarators
,
1424 struct ctf_node
, siblings
);
1427 ret
= visit_field_class_declarator(
1428 ctx
, target
->u
.field_class_alias_target
.field_class_specifier_list
, &qdummy_field_name
,
1429 node
, &class_decl
, NULL
);
1431 BT_ASSERT(!class_decl
);
1432 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot visit field class declarator: ret={}", ret
);
1436 /* Do not allow field class def and alias of untagged variants */
1437 if (class_decl
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
1438 struct ctf_field_class_variant
*var_fc
= ctf_field_class_as_variant(class_decl
);
1440 if (var_fc
->tag_path
.path
->len
== 0) {
1441 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1442 target
, "Type definition of untagged variant field class is not allowed.");
1449 * The semantic validator does not check whether the target is
1450 * abstract or not (if it has an identifier). Check it here.
1452 if (qdummy_field_name
!= 0) {
1453 _BT_CPPLOGE_APPEND_CAUSE_NODE(target
, "Expecting empty identifier: id=\"{}\"",
1454 g_quark_to_string(qdummy_field_name
));
1459 /* Create alias identifier */
1460 node
= _BT_LIST_FIRST_ENTRY(&alias
->u
.field_class_alias_name
.field_class_declarators
,
1461 struct ctf_node
, siblings
);
1462 qalias
= create_class_alias_identifier(
1463 ctx
, alias
->u
.field_class_alias_name
.field_class_specifier_list
, node
);
1464 ret
= ctx_decl_scope_register_alias(ctx
->current_scope
, g_quark_to_string(qalias
), class_decl
);
1466 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot register class alias: name=\"{}\"",
1467 g_quark_to_string(qalias
));
1472 ctf_field_class_destroy(class_decl
);
1477 static int visit_struct_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
1478 struct ctf_field_class_struct
*struct_decl
)
1482 switch (entry_node
->type
) {
1484 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1485 &entry_node
->u
.field_class_def
.field_class_declarators
);
1487 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1488 entry_node
, "Cannot add field class found in structure field class: ret={}", ret
);
1492 case NODE_TYPEALIAS
:
1493 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1494 entry_node
->u
.field_class_alias
.alias
);
1496 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1497 entry_node
, "Cannot add field class alias found in structure field class: ret={}",
1502 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1504 ret
= visit_struct_decl_field(
1506 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1507 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1513 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type={}",
1523 static int visit_variant_decl_entry(struct ctf_visitor_generate_ir
*ctx
,
1524 struct ctf_node
*entry_node
,
1525 struct ctf_field_class_variant
*variant_decl
)
1529 switch (entry_node
->type
) {
1531 ret
= visit_field_class_def(ctx
, entry_node
->u
.field_class_def
.field_class_specifier_list
,
1532 &entry_node
->u
.field_class_def
.field_class_declarators
);
1534 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1535 entry_node
, "Cannot add field class found in variant field class: ret={}", ret
);
1539 case NODE_TYPEALIAS
:
1540 ret
= visit_field_class_alias(ctx
, entry_node
->u
.field_class_alias
.target
,
1541 entry_node
->u
.field_class_alias
.alias
);
1543 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1544 entry_node
, "Cannot add field class alias found in variant field class: ret={}",
1549 case NODE_STRUCT_OR_VARIANT_DECLARATION
:
1551 ret
= visit_variant_decl_field(
1553 entry_node
->u
.struct_or_variant_declaration
.field_class_specifier_list
,
1554 &entry_node
->u
.struct_or_variant_declaration
.field_class_declarators
);
1560 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type={}",
1570 static int visit_struct_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1571 struct bt_list_head
*decl_list
, int has_body
,
1572 struct bt_list_head
*min_align
,
1573 struct ctf_field_class_struct
**struct_decl
)
1577 BT_ASSERT(struct_decl
);
1578 *struct_decl
= NULL
;
1580 /* For named struct (without body), lookup in declaration scope */
1583 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1584 "Bodyless structure field class: missing name.");
1589 *struct_decl
= ctx_decl_scope_lookup_struct(ctx
->current_scope
, name
, -1, true);
1590 if (!*struct_decl
) {
1591 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1592 ctx
->logger
, "Cannot find structure field class: name=\"struct {}\"", name
);
1597 struct ctf_node
*entry_node
;
1598 uint64_t min_align_value
= 0;
1601 if (ctx_decl_scope_lookup_struct(ctx
->current_scope
, name
, 1, false)) {
1602 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1604 "Structure field class already declared in local scope: "
1605 "name=\"struct {}\"",
1612 if (!bt_list_empty(min_align
)) {
1613 ret
= get_unary_unsigned(ctx
, min_align
, &min_align_value
);
1615 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1617 "Unexpected unary expression for structure field class's `align` attribute: "
1624 *struct_decl
= ctf_field_class_struct_create();
1625 BT_ASSERT(*struct_decl
);
1627 if (min_align_value
!= 0) {
1628 (*struct_decl
)->base
.alignment
= min_align_value
;
1631 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1633 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1634 ret
= visit_struct_decl_entry(ctx
, entry_node
, *struct_decl
);
1636 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
,
1637 "Cannot visit structure field class entry: "
1648 ret
= ctx_decl_scope_register_struct(ctx
->current_scope
, name
, *struct_decl
);
1650 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1652 "Cannot register structure field class in declaration scope: "
1653 "name=\"struct {}\", ret={}",
1663 ctf_field_class_destroy(&(*struct_decl
)->base
);
1664 *struct_decl
= NULL
;
1668 static int visit_variant_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1669 const char *tag
, struct bt_list_head
*decl_list
, int has_body
,
1670 struct ctf_field_class_variant
**variant_decl
)
1673 struct ctf_field_class_variant
*untagged_variant_decl
= NULL
;
1675 BT_ASSERT(variant_decl
);
1676 *variant_decl
= NULL
;
1678 /* For named variant (without body), lookup in declaration scope */
1681 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1682 "Bodyless variant field class: missing name.");
1687 untagged_variant_decl
= ctx_decl_scope_lookup_variant(ctx
->current_scope
, name
, -1, true);
1688 if (!untagged_variant_decl
) {
1689 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1690 ctx
->logger
, "Cannot find variant field class: name=\"variant {}\"", name
);
1695 struct ctf_node
*entry_node
;
1698 if (ctx_decl_scope_lookup_variant(ctx
->current_scope
, name
, 1, false)) {
1699 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1700 "Variant field class already declared in local scope: "
1701 "name=\"variant {}\"",
1708 untagged_variant_decl
= ctf_field_class_variant_create();
1709 BT_ASSERT(untagged_variant_decl
);
1710 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
1712 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
1713 ret
= visit_variant_decl_entry(ctx
, entry_node
, untagged_variant_decl
);
1715 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
,
1716 "Cannot visit variant field class entry: "
1727 ret
= ctx_decl_scope_register_variant(ctx
->current_scope
, name
, untagged_variant_decl
);
1729 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1731 "Cannot register variant field class in declaration scope: "
1732 "name=\"variant {}\", ret={}",
1740 * If tagged, create tagged variant and return; otherwise
1741 * return untagged variant.
1744 *variant_decl
= untagged_variant_decl
;
1745 untagged_variant_decl
= NULL
;
1748 * At this point, we have a fresh untagged variant; nobody
1749 * else owns it. Set its tag now.
1751 g_string_assign(untagged_variant_decl
->tag_ref
, tag
);
1752 *variant_decl
= untagged_variant_decl
;
1753 untagged_variant_decl
= NULL
;
1756 BT_ASSERT(!untagged_variant_decl
);
1757 BT_ASSERT(*variant_decl
);
1761 ctf_field_class_destroy(&untagged_variant_decl
->base
);
1762 untagged_variant_decl
= NULL
;
1763 ctf_field_class_destroy(&(*variant_decl
)->base
);
1764 *variant_decl
= NULL
;
1778 static int visit_enum_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*enumerator
,
1779 struct ctf_field_class_enum
*enum_decl
, struct uori
*last
)
1783 struct ctf_node
*iter
;
1784 struct uori start
= {
1798 const char *label
= enumerator
->u
.enumerator
.id
;
1799 struct bt_list_head
*values
= &enumerator
->u
.enumerator
.values
;
1801 bt_list_for_each_entry (iter
, values
, siblings
) {
1802 struct uori
*target
;
1804 if (iter
->type
!= NODE_UNARY_EXPRESSION
) {
1805 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
1806 "Wrong expression for enumeration field class label: "
1807 "node-type={}, label=\"{}\"",
1819 switch (iter
->u
.unary_expression
.type
) {
1820 case UNARY_SIGNED_CONSTANT
:
1821 target
->is_signed
= true;
1822 target
->value
.i
= iter
->u
.unary_expression
.u
.signed_constant
;
1824 case UNARY_UNSIGNED_CONSTANT
:
1825 target
->is_signed
= false;
1826 target
->value
.u
= iter
->u
.unary_expression
.u
.unsigned_constant
;
1829 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
1830 "Invalid enumeration field class entry: "
1831 "expecting constant signed or unsigned integer: "
1832 "node-type={}, label=\"{}\"",
1833 iter
->u
.unary_expression
.type
, label
);
1839 _BT_CPPLOGE_APPEND_CAUSE_NODE(
1840 iter
, "Invalid enumeration field class entry: label=\"{}\"", label
);
1856 if (end
.is_signed
) {
1857 last
->value
.i
= end
.value
.i
+ 1;
1859 last
->value
.u
= end
.value
.u
+ 1;
1862 ctf_field_class_enum_map_range(enum_decl
, label
, start
.value
.u
, end
.value
.u
);
1869 static int visit_enum_decl(struct ctf_visitor_generate_ir
*ctx
, const char *name
,
1870 struct ctf_node
*container_cls
, struct bt_list_head
*enumerator_list
,
1871 int has_body
, struct ctf_field_class_enum
**enum_decl
)
1875 struct ctf_field_class_int
*integer_decl
= NULL
;
1877 BT_ASSERT(enum_decl
);
1880 /* For named enum (without body), lookup in declaration scope */
1883 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1884 "Bodyless enumeration field class: missing name.");
1889 *enum_decl
= ctx_decl_scope_lookup_enum(ctx
->current_scope
, name
, -1, true);
1891 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
1892 "Cannot find enumeration field class: "
1899 struct ctf_node
*iter
;
1900 struct uori last_value
= {
1909 if (ctx_decl_scope_lookup_enum(ctx
->current_scope
, name
, 1, false)) {
1910 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1912 "Enumeration field class already declared in local scope: "
1920 if (!container_cls
) {
1921 integer_decl
= ctf_field_class_as_int(
1922 ctx_decl_scope_lookup_alias(ctx
->current_scope
, "int", -1, true));
1923 if (!integer_decl
) {
1924 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1926 "Cannot find implicit `int` field class alias for enumeration field class.");
1931 ctf_field_class
*decl
;
1933 ret
= visit_field_class_declarator(ctx
, container_cls
, &qdummy_id
, NULL
, &decl
, NULL
);
1940 integer_decl
= ctf_field_class_as_int(decl
);
1943 BT_ASSERT(integer_decl
);
1945 if (integer_decl
->base
.base
.type
!= CTF_FIELD_CLASS_TYPE_INT
) {
1946 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1948 "Container field class for enumeration field class is not an integer field class: "
1950 integer_decl
->base
.base
.type
);
1955 *enum_decl
= ctf_field_class_enum_create();
1956 BT_ASSERT(*enum_decl
);
1957 (*enum_decl
)->base
.base
.base
.alignment
= integer_decl
->base
.base
.alignment
;
1958 ctf_field_class_int_copy_content(&(*enum_decl
)->base
, integer_decl
);
1959 last_value
.is_signed
= (*enum_decl
)->base
.is_signed
;
1961 bt_list_for_each_entry (iter
, enumerator_list
, siblings
) {
1962 ret
= visit_enum_decl_entry(ctx
, iter
, *enum_decl
, &last_value
);
1964 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
1965 "Cannot visit enumeration field class entry: "
1973 ret
= ctx_decl_scope_register_enum(ctx
->current_scope
, name
, *enum_decl
);
1975 BT_CPPLOGE_APPEND_CAUSE_SPEC(
1977 "Cannot register enumeration field class in declaration scope: "
1988 ctf_field_class_destroy(&(*enum_decl
)->base
.base
.base
);
1992 ctf_field_class_destroy(&integer_decl
->base
.base
);
1993 integer_decl
= NULL
;
1997 static int visit_field_class_specifier(struct ctf_visitor_generate_ir
*ctx
,
1998 struct ctf_node
*cls_specifier_list
,
1999 struct ctf_field_class
**decl
)
2002 GString
*str
= NULL
;
2005 str
= g_string_new("");
2006 ret
= get_class_specifier_list_name(ctx
, cls_specifier_list
, str
);
2008 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
2009 "Cannot get field class specifier list's name: ret={}", ret
);
2013 *decl
= ctx_decl_scope_lookup_alias(ctx
->current_scope
, str
->str
, -1, true);
2015 _BT_CPPLOGE_APPEND_CAUSE_NODE(cls_specifier_list
,
2016 "Cannot find field class alias: name=\"{}\"", str
->str
);
2024 ctf_field_class_destroy(*decl
);
2029 g_string_free(str
, TRUE
);
2035 static int visit_integer_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2036 struct ctf_field_class_int
**integer_decl
)
2041 struct ctf_node
*expression
;
2042 uint64_t alignment
= 0, size
= 0;
2043 struct ctf_clock_class
*mapped_clock_class
= NULL
;
2044 enum ctf_encoding encoding
= CTF_ENCODING_NONE
;
2045 bt_field_class_integer_preferred_display_base base
=
2046 BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2047 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2049 *integer_decl
= NULL
;
2051 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2052 struct ctf_node
*left
, *right
;
2054 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2056 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2058 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2059 _BT_CPPLOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type={}",
2060 left
->u
.unary_expression
.type
);
2065 if (strcmp(left
->u
.unary_expression
.u
.string
, "signed") == 0) {
2066 if (_IS_SET(&set
, _INTEGER_SIGNED_SET
)) {
2067 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "signed", "integer field class");
2072 signedness
= get_boolean(ctx
, right
);
2073 if (signedness
< 0) {
2074 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2076 "Invalid boolean value for integer field class's `signed` attribute: "
2083 _SET(&set
, _INTEGER_SIGNED_SET
);
2084 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2085 if (_IS_SET(&set
, _INTEGER_BYTE_ORDER_SET
)) {
2086 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order", "integer field class");
2091 byte_order
= get_real_byte_order(ctx
, right
);
2092 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2093 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2095 "Invalid `byte_order` attribute in integer field class: "
2102 _SET(&set
, _INTEGER_BYTE_ORDER_SET
);
2103 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "size") == 0) {
2104 if (_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2105 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "size", "integer field class");
2110 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2111 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2112 "Invalid `size` attribute in integer field class: "
2113 "expecting unsigned constant integer: "
2115 right
->u
.unary_expression
.type
);
2120 size
= right
->u
.unary_expression
.u
.unsigned_constant
;
2122 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2123 "Invalid `size` attribute in integer field class: "
2124 "expecting positive constant integer: "
2129 } else if (size
> 64) {
2130 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2132 "Invalid `size` attribute in integer field class: "
2133 "integer fields over 64 bits are not supported as of this version: "
2140 _SET(&set
, _INTEGER_SIZE_SET
);
2141 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2142 if (_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2143 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "align", "integer field class");
2148 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2149 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2150 "Invalid `align` attribute in integer field class: "
2151 "expecting unsigned constant integer: "
2153 right
->u
.unary_expression
.type
);
2158 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2159 if (!is_align_valid(alignment
)) {
2160 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2161 "Invalid `align` attribute in integer field class: "
2162 "expecting power of two: "
2169 _SET(&set
, _INTEGER_ALIGN_SET
);
2170 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "base") == 0) {
2171 if (_IS_SET(&set
, _INTEGER_BASE_SET
)) {
2172 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "base", "integer field class");
2177 switch (right
->u
.unary_expression
.type
) {
2178 case UNARY_UNSIGNED_CONSTANT
:
2180 uint64_t constant
= right
->u
.unary_expression
.u
.unsigned_constant
;
2184 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2187 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2190 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2193 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2196 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2198 "Invalid `base` attribute in integer field class: "
2200 right
->u
.unary_expression
.u
.unsigned_constant
);
2209 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2211 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2213 "Unexpected unary expression for integer field class's `base` attribute.");
2218 if (strcmp(s_right
, "decimal") == 0 || strcmp(s_right
, "dec") == 0 ||
2219 strcmp(s_right
, "d") == 0 || strcmp(s_right
, "i") == 0 ||
2220 strcmp(s_right
, "u") == 0) {
2221 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
2222 } else if (strcmp(s_right
, "hexadecimal") == 0 || strcmp(s_right
, "hex") == 0 ||
2223 strcmp(s_right
, "x") == 0 || strcmp(s_right
, "X") == 0 ||
2224 strcmp(s_right
, "p") == 0) {
2225 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
;
2226 } else if (strcmp(s_right
, "octal") == 0 || strcmp(s_right
, "oct") == 0 ||
2227 strcmp(s_right
, "o") == 0) {
2228 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
;
2229 } else if (strcmp(s_right
, "binary") == 0 || strcmp(s_right
, "b") == 0) {
2230 base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
;
2232 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2234 "Unexpected unary expression for integer field class's `base` attribute: "
2246 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2247 right
, "Invalid `base` attribute in integer field class: "
2248 "expecting unsigned constant integer or unary string.");
2253 _SET(&set
, _INTEGER_BASE_SET
);
2254 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2257 if (_IS_SET(&set
, _INTEGER_ENCODING_SET
)) {
2258 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "integer field class");
2263 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2264 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2265 right
, "Invalid `encoding` attribute in integer field class: "
2266 "expecting unary string.");
2271 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2273 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2275 "Unexpected unary expression for integer field class's `encoding` attribute.");
2280 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2281 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2282 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2283 encoding
= CTF_ENCODING_UTF8
;
2284 } else if (strcmp(s_right
, "none") == 0) {
2285 encoding
= CTF_ENCODING_NONE
;
2287 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2289 "Invalid `encoding` attribute in integer field class: "
2290 "unknown encoding: encoding=\"{}\"",
2298 _SET(&set
, _INTEGER_ENCODING_SET
);
2299 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "map") == 0) {
2300 const char *clock_name
;
2302 if (_IS_SET(&set
, _INTEGER_MAP_SET
)) {
2303 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "map", "integer field class");
2308 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2309 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2310 "Invalid `map` attribute in integer field class: "
2311 "expecting unary string.");
2316 clock_name
= get_map_clock_name_value(&expression
->u
.ctf_expression
.right
);
2319 ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2322 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2324 "Unexpected unary expression for integer field class's `map` attribute.");
2329 _BT_CPPLOGE_NODE(right
,
2330 "Invalid `map` attribute in integer field class: "
2331 "cannot find clock class at this point: name=\"{}\"",
2333 _SET(&set
, _INTEGER_MAP_SET
);
2338 mapped_clock_class
=
2339 ctf_trace_class_borrow_clock_class_by_name(ctx
->ctf_tc
, clock_name
);
2340 if (!mapped_clock_class
) {
2341 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2342 "Invalid `map` attribute in integer field class: "
2343 "cannot find clock class at this point: name=\"{}\"",
2349 _SET(&set
, _INTEGER_MAP_SET
);
2351 _BT_CPPLOGW_NODE(left
,
2352 "Unknown attribute in integer field class: "
2354 left
->u
.unary_expression
.u
.string
);
2358 if (!_IS_SET(&set
, _INTEGER_SIZE_SET
)) {
2359 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
2360 "Missing `size` attribute in integer field class.");
2365 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2366 if (size
% CHAR_BIT
) {
2367 /* Bit-packed alignment */
2370 /* Byte-packed alignment */
2371 alignment
= CHAR_BIT
;
2375 *integer_decl
= ctf_field_class_int_create();
2376 BT_ASSERT(*integer_decl
);
2377 (*integer_decl
)->base
.base
.alignment
= alignment
;
2378 (*integer_decl
)->base
.byte_order
= byte_order
;
2379 (*integer_decl
)->base
.size
= size
;
2380 (*integer_decl
)->is_signed
= (signedness
> 0);
2381 (*integer_decl
)->disp_base
= base
;
2382 (*integer_decl
)->encoding
= encoding
;
2383 (*integer_decl
)->mapped_clock_class
= mapped_clock_class
;
2387 ctf_field_class_destroy(&(*integer_decl
)->base
.base
);
2388 *integer_decl
= NULL
;
2392 static int visit_floating_point_number_decl(struct ctf_visitor_generate_ir
*ctx
,
2393 struct bt_list_head
*expressions
,
2394 struct ctf_field_class_float
**float_decl
)
2398 struct ctf_node
*expression
;
2399 uint64_t alignment
= 1, exp_dig
= 0, mant_dig
= 0;
2400 enum ctf_byte_order byte_order
= ctx
->ctf_tc
->default_byte_order
;
2404 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2405 struct ctf_node
*left
, *right
;
2407 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2409 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2411 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2412 _BT_CPPLOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type={}",
2413 left
->u
.unary_expression
.type
);
2418 if (strcmp(left
->u
.unary_expression
.u
.string
, "byte_order") == 0) {
2419 if (_IS_SET(&set
, _FLOAT_BYTE_ORDER_SET
)) {
2420 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "byte_order",
2421 "floating point number field class");
2426 byte_order
= get_real_byte_order(ctx
, right
);
2427 if (byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
2428 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2430 "Invalid `byte_order` attribute in floating point number field class: "
2437 _SET(&set
, _FLOAT_BYTE_ORDER_SET
);
2438 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "exp_dig") == 0) {
2439 if (_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2440 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "exp_dig",
2441 "floating point number field class");
2446 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2447 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2449 "Invalid `exp_dig` attribute in floating point number field class: "
2450 "expecting unsigned constant integer: "
2452 right
->u
.unary_expression
.type
);
2457 exp_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2458 _SET(&set
, _FLOAT_EXP_DIG_SET
);
2459 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "mant_dig") == 0) {
2460 if (_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2461 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "mant_dig",
2462 "floating point number field class");
2467 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2468 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2470 "Invalid `mant_dig` attribute in floating point number field class: "
2471 "expecting unsigned constant integer: "
2473 right
->u
.unary_expression
.type
);
2478 mant_dig
= right
->u
.unary_expression
.u
.unsigned_constant
;
2479 _SET(&set
, _FLOAT_MANT_DIG_SET
);
2480 } else if (strcmp(left
->u
.unary_expression
.u
.string
, "align") == 0) {
2481 if (_IS_SET(&set
, _FLOAT_ALIGN_SET
)) {
2482 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "align",
2483 "floating point number field class");
2488 if (right
->u
.unary_expression
.type
!= UNARY_UNSIGNED_CONSTANT
) {
2489 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2491 "Invalid `align` attribute in floating point number field class: "
2492 "expecting unsigned constant integer: "
2494 right
->u
.unary_expression
.type
);
2499 alignment
= right
->u
.unary_expression
.u
.unsigned_constant
;
2501 if (!is_align_valid(alignment
)) {
2502 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2504 "Invalid `align` attribute in floating point number field class: "
2505 "expecting power of two: "
2512 _SET(&set
, _FLOAT_ALIGN_SET
);
2514 _BT_CPPLOGW_NODE(left
,
2515 "Unknown attribute in floating point number field class: "
2517 left
->u
.unary_expression
.u
.string
);
2521 if (!_IS_SET(&set
, _FLOAT_MANT_DIG_SET
)) {
2522 BT_CPPLOGE_APPEND_CAUSE_SPEC(
2523 ctx
->logger
, "Missing `mant_dig` attribute in floating point number field class.");
2528 if (!_IS_SET(&set
, _FLOAT_EXP_DIG_SET
)) {
2529 BT_CPPLOGE_APPEND_CAUSE_SPEC(
2530 ctx
->logger
, "Missing `exp_dig` attribute in floating point number field class.");
2535 if (mant_dig
!= 24 && mant_dig
!= 53) {
2536 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
, "`mant_dig` attribute: expecting 24 or 53.");
2541 if (mant_dig
== 24 && exp_dig
!= 8) {
2542 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
2543 "`exp_dig` attribute: expecting 8 because `mant_dig` is 24.");
2548 if (mant_dig
== 53 && exp_dig
!= 11) {
2549 BT_CPPLOGE_APPEND_CAUSE_SPEC(ctx
->logger
,
2550 "`exp_dig` attribute: expecting 11 because `mant_dig` is 53.");
2555 if (!_IS_SET(&set
, _INTEGER_ALIGN_SET
)) {
2556 if ((mant_dig
+ exp_dig
) % CHAR_BIT
) {
2557 /* Bit-packed alignment */
2560 /* Byte-packed alignment */
2561 alignment
= CHAR_BIT
;
2565 *float_decl
= ctf_field_class_float_create();
2566 BT_ASSERT(*float_decl
);
2567 (*float_decl
)->base
.base
.alignment
= alignment
;
2568 (*float_decl
)->base
.byte_order
= byte_order
;
2569 (*float_decl
)->base
.size
= mant_dig
+ exp_dig
;
2573 ctf_field_class_destroy(&(*float_decl
)->base
.base
);
2578 static int visit_string_decl(struct ctf_visitor_generate_ir
*ctx
, struct bt_list_head
*expressions
,
2579 struct ctf_field_class_string
**string_decl
)
2583 struct ctf_node
*expression
;
2584 enum ctf_encoding encoding
= CTF_ENCODING_UTF8
;
2586 *string_decl
= NULL
;
2588 bt_list_for_each_entry (expression
, expressions
, siblings
) {
2589 struct ctf_node
*left
, *right
;
2591 left
= _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.left
, struct ctf_node
, siblings
);
2593 _BT_LIST_FIRST_ENTRY(&expression
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
2595 if (left
->u
.unary_expression
.type
!= UNARY_STRING
) {
2596 _BT_CPPLOGE_APPEND_CAUSE_NODE(left
, "Unexpected unary expression type: type={}",
2597 left
->u
.unary_expression
.type
);
2602 if (strcmp(left
->u
.unary_expression
.u
.string
, "encoding") == 0) {
2605 if (_IS_SET(&set
, _STRING_ENCODING_SET
)) {
2606 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(left
, "encoding", "string field class");
2611 if (right
->u
.unary_expression
.type
!= UNARY_STRING
) {
2612 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2613 "Invalid `encoding` attribute in string field class: "
2614 "expecting unary string.");
2619 s_right
= ctf_ast_concatenate_unary_strings(&expression
->u
.ctf_expression
.right
);
2621 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2623 "Unexpected unary expression for string field class's `encoding` attribute.");
2628 if (strcmp(s_right
, "UTF8") == 0 || strcmp(s_right
, "utf8") == 0 ||
2629 strcmp(s_right
, "utf-8") == 0 || strcmp(s_right
, "UTF-8") == 0 ||
2630 strcmp(s_right
, "ASCII") == 0 || strcmp(s_right
, "ascii") == 0) {
2631 encoding
= CTF_ENCODING_UTF8
;
2632 } else if (strcmp(s_right
, "none") == 0) {
2633 encoding
= CTF_ENCODING_NONE
;
2635 _BT_CPPLOGE_APPEND_CAUSE_NODE(right
,
2636 "Invalid `encoding` attribute in string field class: "
2637 "unknown encoding: encoding=\"{}\"",
2645 _SET(&set
, _STRING_ENCODING_SET
);
2647 _BT_CPPLOGW_NODE(left
,
2648 "Unknown attribute in string field class: "
2650 left
->u
.unary_expression
.u
.string
);
2654 *string_decl
= ctf_field_class_string_create();
2655 BT_ASSERT(*string_decl
);
2656 (*string_decl
)->encoding
= encoding
;
2660 ctf_field_class_destroy(&(*string_decl
)->base
);
2661 *string_decl
= NULL
;
2665 static int visit_field_class_specifier_list(struct ctf_visitor_generate_ir
*ctx
,
2666 struct ctf_node
*ts_list
, struct ctf_field_class
**decl
)
2669 struct ctf_node
*first
, *node
;
2673 if (ts_list
->type
!= NODE_TYPE_SPECIFIER_LIST
) {
2674 _BT_CPPLOGE_APPEND_CAUSE_NODE(ts_list
, "Unexpected node type: node-type={}", ts_list
->type
);
2679 first
= _BT_LIST_FIRST_ENTRY(&ts_list
->u
.field_class_specifier_list
.head
, struct ctf_node
,
2681 if (first
->type
!= NODE_TYPE_SPECIFIER
) {
2682 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Unexpected node type: node-type={}", first
->type
);
2687 node
= first
->u
.field_class_specifier
.node
;
2689 switch (first
->u
.field_class_specifier
.type
) {
2690 case TYPESPEC_INTEGER
:
2692 ctf_field_class_int
*int_decl
;
2694 ret
= visit_integer_decl(ctx
, &node
->u
.integer
.expressions
, &int_decl
);
2696 BT_ASSERT(!int_decl
);
2700 *decl
= &int_decl
->base
.base
;
2703 case TYPESPEC_FLOATING_POINT
:
2705 ctf_field_class_float
*float_decl
;
2708 visit_floating_point_number_decl(ctx
, &node
->u
.floating_point
.expressions
, &float_decl
);
2710 BT_ASSERT(!float_decl
);
2714 *decl
= &float_decl
->base
.base
;
2717 case TYPESPEC_STRING
:
2719 ctf_field_class_string
*string_decl
;
2721 ret
= visit_string_decl(ctx
, &node
->u
.string
.expressions
, &string_decl
);
2723 BT_ASSERT(!string_decl
);
2727 *decl
= &string_decl
->base
;
2730 case TYPESPEC_STRUCT
:
2732 ctf_field_class_struct
*struct_decl
;
2734 ret
= visit_struct_decl(ctx
, node
->u
._struct
.name
, &node
->u
._struct
.declaration_list
,
2735 node
->u
._struct
.has_body
, &node
->u
._struct
.min_align
, &struct_decl
);
2737 BT_ASSERT(!struct_decl
);
2741 *decl
= &struct_decl
->base
;
2744 case TYPESPEC_VARIANT
:
2746 ctf_field_class_variant
*variant_decl
;
2748 ret
= visit_variant_decl(ctx
, node
->u
.variant
.name
, node
->u
.variant
.choice
,
2749 &node
->u
.variant
.declaration_list
, node
->u
.variant
.has_body
,
2752 BT_ASSERT(!variant_decl
);
2756 *decl
= &variant_decl
->base
;
2761 ctf_field_class_enum
*enum_decl
;
2763 ret
= visit_enum_decl(ctx
, node
->u
._enum
.enum_id
, node
->u
._enum
.container_field_class
,
2764 &node
->u
._enum
.enumerator_list
, node
->u
._enum
.has_body
, &enum_decl
);
2766 BT_ASSERT(!enum_decl
);
2770 *decl
= &enum_decl
->base
.base
.base
;
2775 case TYPESPEC_SHORT
:
2778 case TYPESPEC_FLOAT
:
2779 case TYPESPEC_DOUBLE
:
2780 case TYPESPEC_SIGNED
:
2781 case TYPESPEC_UNSIGNED
:
2783 case TYPESPEC_COMPLEX
:
2784 case TYPESPEC_IMAGINARY
:
2785 case TYPESPEC_CONST
:
2786 case TYPESPEC_ID_TYPE
:
2787 ret
= visit_field_class_specifier(ctx
, ts_list
, decl
);
2789 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Cannot visit field class specifier: ret={}", ret
);
2795 _BT_CPPLOGE_APPEND_CAUSE_NODE(first
, "Unexpected field class specifier type: node-type={}",
2796 first
->u
.field_class_specifier
.type
);
2805 ctf_field_class_destroy(*decl
);
2810 static int visit_event_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
2811 struct ctf_event_class
*event_class
, uint64_t *stream_id
,
2817 switch (node
->type
) {
2819 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
2820 &node
->u
.field_class_def
.field_class_declarators
);
2822 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in event class.");
2826 case NODE_TYPEALIAS
:
2827 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
2828 node
->u
.field_class_alias
.alias
);
2830 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
2831 "Cannot add field class alias found in event class.");
2835 case NODE_CTF_EXPRESSION
:
2837 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
2839 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
2844 if (strcmp(left
, "name") == 0) {
2845 /* This is already known at this stage */
2846 if (_IS_SET(set
, _EVENT_NAME_SET
)) {
2847 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "name", "event class");
2852 _SET(set
, _EVENT_NAME_SET
);
2853 } else if (strcmp(left
, "id") == 0) {
2856 if (_IS_SET(set
, _EVENT_ID_SET
)) {
2857 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "event class");
2862 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
2863 /* Only read "id" if get_unary_unsigned() succeeded. */
2864 if (ret
|| (!ret
&& id
< 0)) {
2865 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2866 node
, "Unexpected unary expression for event class's `id` attribute.");
2871 event_class
->id
= id
;
2872 _SET(set
, _EVENT_ID_SET
);
2873 } else if (strcmp(left
, "stream_id") == 0) {
2874 if (_IS_SET(set
, _EVENT_STREAM_ID_SET
)) {
2875 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "stream_id", "event class");
2880 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, stream_id
);
2883 * Only read "stream_id" if get_unary_unsigned()
2887 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2888 node
, "Unexpected unary expression for event class's `stream_id` attribute.");
2893 _SET(set
, _EVENT_STREAM_ID_SET
);
2894 } else if (strcmp(left
, "context") == 0) {
2895 if (_IS_SET(set
, _EVENT_CONTEXT_SET
)) {
2896 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate `context` entry in event class.");
2901 ret
= visit_field_class_specifier_list(
2902 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2903 &event_class
->spec_context_fc
);
2905 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
2906 "Cannot create event class's context field class.");
2910 BT_ASSERT(event_class
->spec_context_fc
);
2911 _SET(set
, _EVENT_CONTEXT_SET
);
2912 } else if (strcmp(left
, "fields") == 0) {
2913 if (_IS_SET(set
, _EVENT_FIELDS_SET
)) {
2914 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate `fields` entry in event class.");
2919 ret
= visit_field_class_specifier_list(
2920 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
2921 &event_class
->payload_fc
);
2923 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
2924 "Cannot create event class's payload field class.");
2928 BT_ASSERT(event_class
->payload_fc
);
2929 _SET(set
, _EVENT_FIELDS_SET
);
2930 } else if (strcmp(left
, "loglevel") == 0) {
2931 uint64_t loglevel_value
;
2932 bool is_log_level_known
= true;
2933 bt_event_class_log_level log_level
;
2935 if (_IS_SET(set
, _EVENT_LOG_LEVEL_SET
)) {
2936 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "loglevel", "event class");
2941 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &loglevel_value
);
2943 _BT_CPPLOGE_APPEND_CAUSE_NODE(
2944 node
, "Unexpected unary expression for event class's `loglevel` attribute.");
2949 switch (loglevel_value
) {
2951 log_level
= BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
;
2954 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ALERT
;
2957 log_level
= BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
;
2960 log_level
= BT_EVENT_CLASS_LOG_LEVEL_ERROR
;
2963 log_level
= BT_EVENT_CLASS_LOG_LEVEL_WARNING
;
2966 log_level
= BT_EVENT_CLASS_LOG_LEVEL_NOTICE
;
2969 log_level
= BT_EVENT_CLASS_LOG_LEVEL_INFO
;
2972 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
;
2975 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
;
2978 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
;
2981 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
;
2984 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
;
2987 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
;
2990 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
;
2993 log_level
= BT_EVENT_CLASS_LOG_LEVEL_DEBUG
;
2996 is_log_level_known
= false;
2999 "Not setting event class's log level because its value is unknown: "
3004 if (is_log_level_known
) {
3005 ctf_event_class_set_log_level(event_class
, log_level
);
3008 _SET(set
, _EVENT_LOG_LEVEL_SET
);
3009 } else if (strcmp(left
, "model.emf.uri") == 0) {
3012 if (_IS_SET(set
, _EVENT_MODEL_EMF_URI_SET
)) {
3013 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "model.emf.uri", "event class");
3018 right
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.right
);
3020 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3022 "Unexpected unary expression for event class's `model.emf.uri` attribute.");
3027 if (strlen(right
) == 0) {
3028 _BT_CPPLOGW_NODE(node
, "Not setting event class's EMF URI because it's empty.");
3030 g_string_assign(event_class
->emf_uri
, right
);
3034 _SET(set
, _EVENT_MODEL_EMF_URI_SET
);
3036 _BT_CPPLOGW_NODE(node
,
3037 "Unknown attribute in event class: "
3060 static char *get_event_decl_name(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3064 struct ctf_node
*iter
;
3065 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3067 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3068 if (iter
->type
!= NODE_CTF_EXPRESSION
) {
3072 left
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.left
);
3074 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot concatenate unary strings.");
3078 if (strcmp(left
, "name") == 0) {
3079 name
= ctf_ast_concatenate_unary_strings(&iter
->u
.ctf_expression
.right
);
3081 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3082 iter
, "Unexpected unary expression for event class's `name` attribute.");
3102 static int visit_event_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3106 struct ctf_node
*iter
;
3107 uint64_t stream_id
= 0;
3108 char *event_name
= NULL
;
3109 struct ctf_event_class
*event_class
= NULL
;
3110 struct ctf_stream_class
*stream_class
= NULL
;
3111 struct bt_list_head
*decl_list
= &node
->u
.event
.declaration_list
;
3112 bool pop_scope
= false;
3114 if (node
->visited
) {
3118 node
->visited
= TRUE
;
3119 event_name
= get_event_decl_name(ctx
, node
);
3121 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `name` attribute in event class.");
3126 event_class
= ctf_event_class_create();
3127 BT_ASSERT(event_class
);
3128 g_string_assign(event_class
->name
, event_name
);
3129 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3132 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3133 ret
= visit_event_decl_entry(ctx
, iter
, event_class
, &stream_id
, &set
);
3135 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
3136 "Cannot visit event class's entry: "
3143 if (!_IS_SET(&set
, _EVENT_STREAM_ID_SET
)) {
3145 * Allow missing stream_id if there is only a single
3148 switch (ctx
->ctf_tc
->stream_classes
->len
) {
3150 /* Create implicit stream class if there's none */
3152 stream_class
= ctf_stream_class_create();
3153 BT_ASSERT(stream_class
);
3154 stream_class
->id
= stream_id
;
3155 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3158 /* Single stream class: get its ID */
3159 stream_class
= (ctf_stream_class
*) ctx
->ctf_tc
->stream_classes
->pdata
[0];
3160 stream_id
= stream_class
->id
;
3163 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `stream_id` attribute in event class.");
3169 /* We have the stream ID now; get the stream class if found */
3170 if (!stream_class
) {
3171 stream_class
= ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_id
);
3172 if (!stream_class
) {
3173 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3174 "Cannot find stream class at this point: "
3182 BT_ASSERT(stream_class
);
3184 if (!_IS_SET(&set
, _EVENT_ID_SET
)) {
3185 /* Allow only one event without ID per stream */
3186 if (stream_class
->event_classes
->len
!= 0) {
3187 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `id` attribute in event class.");
3193 event_class
->id
= 0;
3196 if (ctf_stream_class_borrow_event_class_by_id(stream_class
, event_class
->id
)) {
3197 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3198 "Duplicate event class (same ID) in the same stream class: "
3205 ctf_stream_class_append_event_class(stream_class
, event_class
);
3210 ctf_event_class_destroy(event_class
);
3227 static int auto_map_field_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3228 struct ctf_field_class
*fc
)
3230 struct ctf_clock_class
*clock_class_to_map_to
= NULL
;
3231 uint64_t clock_class_count
;
3237 if (fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&& fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3241 ctf_field_class_int
*int_fc
= ctf_field_class_as_int(fc
);
3243 if (int_fc
->mapped_clock_class
) {
3244 /* Already mapped */
3248 clock_class_count
= ctx
->ctf_tc
->clock_classes
->len
;
3250 switch (clock_class_count
) {
3253 * No clock class exists in the trace at this point. Create an
3254 * implicit one at 1 GHz, named `default`, and use this clock
3257 clock_class_to_map_to
= ctf_clock_class_create();
3258 BT_ASSERT(clock_class_to_map_to
);
3259 clock_class_to_map_to
->frequency
= UINT64_C(1000000000);
3260 g_string_assign(clock_class_to_map_to
->name
, "default");
3261 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock_class_to_map_to
);
3265 * Only one clock class exists in the trace at this point: use
3268 clock_class_to_map_to
= (ctf_clock_class
*) ctx
->ctf_tc
->clock_classes
->pdata
[0];
3272 * Timestamp field not mapped to a clock class and there's more
3273 * than one clock class in the trace: this is an error.
3275 BT_CPPLOGE_APPEND_CAUSE_SPEC(
3276 ctx
->logger
, "Timestamp field found with no mapped clock class, "
3277 "but there's more than one clock class in the trace at this point.");
3281 BT_ASSERT(clock_class_to_map_to
);
3282 int_fc
->mapped_clock_class
= clock_class_to_map_to
;
3287 static int auto_map_fields_to_trace_clock_class(struct ctf_visitor_generate_ir
*ctx
,
3288 struct ctf_field_class
*root_fc
,
3289 const char *field_name
)
3293 struct ctf_field_class_struct
*struct_fc
= (ctf_field_class_struct
*) root_fc
;
3294 struct ctf_field_class_variant
*var_fc
= (ctf_field_class_variant
*) root_fc
;
3300 if (root_fc
->type
!= CTF_FIELD_CLASS_TYPE_STRUCT
&&
3301 root_fc
->type
!= CTF_FIELD_CLASS_TYPE_VARIANT
) {
3305 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3306 count
= struct_fc
->members
->len
;
3308 count
= var_fc
->options
->len
;
3311 for (i
= 0; i
< count
; i
++) {
3312 struct ctf_named_field_class
*named_fc
= NULL
;
3314 if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_STRUCT
) {
3315 named_fc
= ctf_field_class_struct_borrow_member_by_index(struct_fc
, i
);
3316 } else if (root_fc
->type
== CTF_FIELD_CLASS_TYPE_VARIANT
) {
3317 named_fc
= ctf_field_class_variant_borrow_option_by_index(var_fc
, i
);
3322 if (strcmp(named_fc
->name
->str
, field_name
) == 0) {
3323 ret
= auto_map_field_to_trace_clock_class(ctx
, named_fc
->fc
);
3325 BT_CPPLOGE_APPEND_CAUSE_SPEC(
3327 "Cannot automatically map field to trace's clock class: "
3328 "field-name=\"{}\"",
3334 ret
= auto_map_fields_to_trace_clock_class(ctx
, named_fc
->fc
, field_name
);
3336 BT_CPPLOGE_APPEND_CAUSE_SPEC(
3338 "Cannot automatically map structure or variant field class's fields to trace's clock class: "
3339 "field-name=\"{}\", root-field-name=\"{}\"",
3340 field_name
, named_fc
->name
->str
);
3349 static int visit_stream_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3350 struct ctf_stream_class
*stream_class
, int *set
)
3355 switch (node
->type
) {
3357 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3358 &node
->u
.field_class_def
.field_class_declarators
);
3360 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot add field class found in stream class.");
3364 case NODE_TYPEALIAS
:
3365 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3366 node
->u
.field_class_alias
.alias
);
3368 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3369 "Cannot add field class alias found in stream class.");
3373 case NODE_CTF_EXPRESSION
:
3375 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3377 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3382 if (strcmp(left
, "id") == 0) {
3385 if (_IS_SET(set
, _STREAM_ID_SET
)) {
3386 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "id", "stream declaration");
3391 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, (uint64_t *) &id
);
3393 /* Only read "id" if get_unary_unsigned() succeeded. */
3394 if (ret
|| (!ret
&& id
< 0)) {
3395 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3396 node
, "Unexpected unary expression for stream class's `id` attribute.");
3401 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, id
)) {
3402 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate stream class (same ID): id={}", id
);
3407 stream_class
->id
= id
;
3408 _SET(set
, _STREAM_ID_SET
);
3409 } else if (strcmp(left
, "event.header") == 0) {
3410 if (_IS_SET(set
, _STREAM_EVENT_HEADER_SET
)) {
3411 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3412 "Duplicate `event.header` entry in stream class.");
3417 ret
= visit_field_class_specifier_list(
3418 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3419 &stream_class
->event_header_fc
);
3421 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3422 node
, "Cannot create stream class's event header field class.");
3426 BT_ASSERT(stream_class
->event_header_fc
);
3427 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->event_header_fc
,
3430 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3432 "Cannot automatically map specific event header field class fields named `timestamp` to trace's clock class.");
3436 _SET(set
, _STREAM_EVENT_HEADER_SET
);
3437 } else if (strcmp(left
, "event.context") == 0) {
3438 if (_IS_SET(set
, _STREAM_EVENT_CONTEXT_SET
)) {
3439 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3440 "Duplicate `event.context` entry in stream class.");
3445 ret
= visit_field_class_specifier_list(
3446 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3447 &stream_class
->event_common_context_fc
);
3449 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3450 node
, "Cannot create stream class's event context field class.");
3454 BT_ASSERT(stream_class
->event_common_context_fc
);
3455 _SET(set
, _STREAM_EVENT_CONTEXT_SET
);
3456 } else if (strcmp(left
, "packet.context") == 0) {
3457 if (_IS_SET(set
, _STREAM_PACKET_CONTEXT_SET
)) {
3458 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3459 "Duplicate `packet.context` entry in stream class.");
3464 ret
= visit_field_class_specifier_list(
3465 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3466 &stream_class
->packet_context_fc
);
3468 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3469 node
, "Cannot create stream class's packet context field class.");
3473 BT_ASSERT(stream_class
->packet_context_fc
);
3474 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3477 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3479 "Cannot automatically map specific packet context field class fields named `timestamp_begin` to trace's clock class.");
3483 ret
= auto_map_fields_to_trace_clock_class(ctx
, stream_class
->packet_context_fc
,
3486 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3488 "Cannot automatically map specific packet context field class fields named `timestamp_end` to trace's clock class.");
3492 _SET(set
, _STREAM_PACKET_CONTEXT_SET
);
3494 _BT_CPPLOGW_NODE(node
,
3495 "Unknown attribute in stream class: "
3517 static int visit_stream_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3521 struct ctf_node
*iter
;
3522 struct ctf_stream_class
*stream_class
= NULL
;
3523 struct bt_list_head
*decl_list
= &node
->u
.stream
.declaration_list
;
3525 if (node
->visited
) {
3529 node
->visited
= TRUE
;
3530 stream_class
= ctf_stream_class_create();
3531 BT_ASSERT(stream_class
);
3532 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3534 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3535 ret
= visit_stream_decl_entry(ctx
, iter
, stream_class
, &set
);
3537 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
3538 "Cannot visit stream class's entry: "
3548 if (_IS_SET(&set
, _STREAM_ID_SET
)) {
3549 /* Check that packet header has `stream_id` field */
3550 struct ctf_named_field_class
*named_fc
= NULL
;
3552 if (!ctx
->ctf_tc
->packet_header_fc
) {
3553 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Stream class has a `id` attribute, "
3554 "but trace has no packet header field class.");
3559 named_fc
= ctf_field_class_struct_borrow_member_by_name(
3560 ctf_field_class_as_struct(ctx
->ctf_tc
->packet_header_fc
), "stream_id");
3562 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3563 node
, "Stream class has a `id` attribute, "
3564 "but trace's packet header field class has no `stream_id` field.");
3569 if (named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_INT
&&
3570 named_fc
->fc
->type
!= CTF_FIELD_CLASS_TYPE_ENUM
) {
3571 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3573 "Stream class has a `id` attribute, "
3574 "but trace's packet header field class's `stream_id` field is not an integer field class.");
3579 /* Allow only _one_ ID-less stream */
3580 if (ctx
->ctf_tc
->stream_classes
->len
!= 0) {
3581 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3583 "Missing `id` attribute in stream class as there's more than one stream class in the trace.");
3588 /* Automatic ID: 0 */
3589 stream_class
->id
= 0;
3593 * Make sure that this stream class's ID is currently unique in
3596 if (ctf_trace_class_borrow_stream_class_by_id(ctx
->ctf_tc
, stream_class
->id
)) {
3597 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate stream class (same ID): id={}",
3603 g_ptr_array_add(ctx
->ctf_tc
->stream_classes
, stream_class
);
3604 stream_class
= NULL
;
3608 ctf_stream_class_destroy(stream_class
);
3609 stream_class
= NULL
;
3615 static int visit_trace_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
,
3622 switch (node
->type
) {
3624 ret
= visit_field_class_def(ctx
, node
->u
.field_class_def
.field_class_specifier_list
,
3625 &node
->u
.field_class_def
.field_class_declarators
);
3627 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3628 "Cannot add field class found in trace (`trace` block).");
3632 case NODE_TYPEALIAS
:
3633 ret
= visit_field_class_alias(ctx
, node
->u
.field_class_alias
.target
,
3634 node
->u
.field_class_alias
.alias
);
3636 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3637 node
, "Cannot add field class alias found in trace (`trace` block).");
3641 case NODE_CTF_EXPRESSION
:
3643 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3645 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3650 if (strcmp(left
, "major") == 0) {
3651 if (_IS_SET(set
, _TRACE_MAJOR_SET
)) {
3652 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "major", "trace");
3657 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3659 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3660 node
, "Unexpected unary expression for trace's `major` attribute.");
3666 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3667 "Invalid trace's `minor` attribute: expecting 1.");
3672 ctx
->ctf_tc
->major
= val
;
3673 _SET(set
, _TRACE_MAJOR_SET
);
3674 } else if (strcmp(left
, "minor") == 0) {
3675 if (_IS_SET(set
, _TRACE_MINOR_SET
)) {
3676 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "minor", "trace");
3681 ret
= get_unary_unsigned(ctx
, &node
->u
.ctf_expression
.right
, &val
);
3683 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3684 node
, "Unexpected unary expression for trace's `minor` attribute.");
3690 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3691 "Invalid trace's `minor` attribute: expecting 8.");
3696 ctx
->ctf_tc
->minor
= val
;
3697 _SET(set
, _TRACE_MINOR_SET
);
3698 } else if (strcmp(left
, "uuid") == 0) {
3699 if (_IS_SET(set
, _TRACE_UUID_SET
)) {
3700 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "uuid", "trace");
3705 ret
= get_unary_uuid(ctx
, &node
->u
.ctf_expression
.right
, ctx
->ctf_tc
->uuid
);
3707 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Invalid trace's `uuid` attribute.");
3711 ctx
->ctf_tc
->is_uuid_set
= true;
3712 _SET(set
, _TRACE_UUID_SET
);
3713 } else if (strcmp(left
, "byte_order") == 0) {
3714 /* Default byte order is already known at this stage */
3715 if (_IS_SET(set
, _TRACE_BYTE_ORDER_SET
)) {
3716 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3721 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
!= CTF_BYTE_ORDER_UNKNOWN
);
3722 _SET(set
, _TRACE_BYTE_ORDER_SET
);
3723 } else if (strcmp(left
, "packet.header") == 0) {
3724 if (_IS_SET(set
, _TRACE_PACKET_HEADER_SET
)) {
3725 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate `packet.header` entry in trace.");
3730 ret
= visit_field_class_specifier_list(
3731 ctx
, _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
),
3732 &ctx
->ctf_tc
->packet_header_fc
);
3734 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3735 "Cannot create trace's packet header field class.");
3739 BT_ASSERT(ctx
->ctf_tc
->packet_header_fc
);
3740 _SET(set
, _TRACE_PACKET_HEADER_SET
);
3742 _BT_CPPLOGW_NODE(node
,
3743 "Unknown attribute in stream class: "
3753 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unknown expression in trace.");
3765 static int visit_trace_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3769 struct ctf_node
*iter
;
3770 struct bt_list_head
*decl_list
= &node
->u
.trace
.declaration_list
;
3772 if (node
->visited
) {
3776 node
->visited
= TRUE
;
3778 if (ctx
->is_trace_visited
) {
3779 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
3784 _TRY_PUSH_SCOPE_OR_GOTO_ERROR();
3786 bt_list_for_each_entry (iter
, decl_list
, siblings
) {
3787 ret
= visit_trace_decl_entry(ctx
, iter
, &set
);
3789 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
3790 "Cannot visit trace's entry (`trace` block): "
3800 if (!_IS_SET(&set
, _TRACE_MAJOR_SET
)) {
3801 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `major` attribute in trace (`trace` block).");
3806 if (!_IS_SET(&set
, _TRACE_MINOR_SET
)) {
3807 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Missing `minor` attribute in trace (`trace` block).");
3812 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3813 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
3814 "Missing `byte_order` attribute in trace (`trace` block).");
3819 ctx
->is_trace_visited
= true;
3828 static int visit_env(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
3832 struct ctf_node
*entry_node
;
3833 struct bt_list_head
*decl_list
= &node
->u
.env
.declaration_list
;
3835 if (node
->visited
) {
3839 node
->visited
= TRUE
;
3841 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
3842 struct bt_list_head
*right_head
= &entry_node
->u
.ctf_expression
.right
;
3844 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
3845 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
,
3846 "Wrong expression in environment entry: "
3853 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
3855 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot get environment entry's name.");
3860 if (is_unary_string(right_head
)) {
3861 char *right
= ctf_ast_concatenate_unary_strings(right_head
);
3864 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3866 "Unexpected unary expression for environment entry's value: "
3873 if (strcmp(left
, "tracer_name") == 0) {
3874 if (strncmp(right
, "lttng", 5) == 0) {
3875 BT_CPPLOGI_SPEC(ctx
->logger
,
3876 "Detected LTTng trace from `{}` environment value: "
3877 "tracer-name=\"{}\"",
3879 ctx
->is_lttng
= true;
3883 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR
,
3886 } else if (is_unary_unsigned(right_head
) || is_unary_signed(right_head
)) {
3889 if (is_unary_unsigned(right_head
)) {
3890 ret
= get_unary_unsigned(ctx
, right_head
, (uint64_t *) &v
);
3892 ret
= get_unary_signed(right_head
, &v
);
3895 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3897 "Unexpected unary expression for environment entry's value: "
3904 ctf_trace_class_append_env_entry(ctx
->ctf_tc
, left
, CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT
,
3907 _BT_CPPLOGW_NODE(entry_node
,
3908 "Environment entry has unknown type: "
3925 static int set_trace_byte_order(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*trace_node
)
3930 struct ctf_node
*node
;
3931 struct bt_list_head
*decl_list
= &trace_node
->u
.trace
.declaration_list
;
3933 bt_list_for_each_entry (node
, decl_list
, siblings
) {
3934 if (node
->type
== NODE_CTF_EXPRESSION
) {
3935 struct ctf_node
*right_node
;
3937 left
= ctf_ast_concatenate_unary_strings(&node
->u
.ctf_expression
.left
);
3939 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Cannot concatenate unary strings.");
3944 if (strcmp(left
, "byte_order") == 0) {
3945 enum ctf_byte_order bo
;
3947 if (_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3948 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(node
, "byte_order", "trace");
3953 _SET(&set
, _TRACE_BYTE_ORDER_SET
);
3955 _BT_LIST_FIRST_ENTRY(&node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
3956 bo
= byte_order_from_unary_expr(ctx
, right_node
);
3957 if (bo
== CTF_BYTE_ORDER_UNKNOWN
) {
3958 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3959 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
3960 "expecting `le`, `be`, or `network`.");
3963 } else if (bo
== CTF_BYTE_ORDER_DEFAULT
) {
3964 _BT_CPPLOGE_APPEND_CAUSE_NODE(
3965 node
, "Invalid `byte_order` attribute in trace (`trace` block): "
3966 "cannot be set to `native` here.");
3971 ctx
->ctf_tc
->default_byte_order
= bo
;
3979 if (!_IS_SET(&set
, _TRACE_BYTE_ORDER_SET
)) {
3980 _BT_CPPLOGE_APPEND_CAUSE_NODE(trace_node
,
3981 "Missing `byte_order` attribute in trace (`trace` block).");
3993 static int visit_clock_decl_entry(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*entry_node
,
3994 struct ctf_clock_class
*clock
, int *set
, int64_t *offset_seconds
,
3995 uint64_t *offset_cycles
)
4000 if (entry_node
->type
!= NODE_CTF_EXPRESSION
) {
4001 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Unexpected node type: node-type={}",
4007 left
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.left
);
4009 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot concatenate unary strings.");
4014 if (strcmp(left
, "name") == 0) {
4017 if (_IS_SET(set
, _CLOCK_NAME_SET
)) {
4018 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "name", "clock class");
4023 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4025 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4026 entry_node
, "Unexpected unary expression for clock class's `name` attribute.");
4031 g_string_assign(clock
->name
, right
);
4033 _SET(set
, _CLOCK_NAME_SET
);
4034 } else if (strcmp(left
, "uuid") == 0) {
4037 if (_IS_SET(set
, _CLOCK_UUID_SET
)) {
4038 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "uuid", "clock class");
4043 ret
= get_unary_uuid(ctx
, &entry_node
->u
.ctf_expression
.right
, uuid
);
4045 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Invalid clock class's `uuid` attribute.");
4049 clock
->has_uuid
= true;
4050 bt_uuid_copy(clock
->uuid
, uuid
);
4051 _SET(set
, _CLOCK_UUID_SET
);
4052 } else if (strcmp(left
, "description") == 0) {
4055 if (_IS_SET(set
, _CLOCK_DESCRIPTION_SET
)) {
4056 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "description", "clock class");
4061 right
= ctf_ast_concatenate_unary_strings(&entry_node
->u
.ctf_expression
.right
);
4063 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4065 "Unexpected unary expression for clock class's `description` attribute.");
4070 g_string_assign(clock
->description
, right
);
4072 _SET(set
, _CLOCK_DESCRIPTION_SET
);
4073 } else if (strcmp(left
, "freq") == 0) {
4074 uint64_t freq
= UINT64_C(-1);
4076 if (_IS_SET(set
, _CLOCK_FREQ_SET
)) {
4077 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "freq", "clock class");
4082 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &freq
);
4084 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4085 entry_node
, "Unexpected unary expression for clock class's `freq` attribute.");
4090 if (freq
== UINT64_C(-1) || freq
== 0) {
4091 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Invalid clock class frequency: freq={}",
4097 clock
->frequency
= freq
;
4098 _SET(set
, _CLOCK_FREQ_SET
);
4099 } else if (strcmp(left
, "precision") == 0) {
4102 if (_IS_SET(set
, _CLOCK_PRECISION_SET
)) {
4103 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "precision", "clock class");
4108 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, &precision
);
4110 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4111 entry_node
, "Unexpected unary expression for clock class's `precision` attribute.");
4116 clock
->precision
= precision
;
4117 _SET(set
, _CLOCK_PRECISION_SET
);
4118 } else if (strcmp(left
, "offset_s") == 0) {
4119 if (_IS_SET(set
, _CLOCK_OFFSET_S_SET
)) {
4120 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset_s", "clock class");
4125 ret
= get_unary_signed(&entry_node
->u
.ctf_expression
.right
, offset_seconds
);
4127 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4128 entry_node
, "Unexpected unary expression for clock class's `offset_s` attribute.");
4133 _SET(set
, _CLOCK_OFFSET_S_SET
);
4134 } else if (strcmp(left
, "offset") == 0) {
4135 if (_IS_SET(set
, _CLOCK_OFFSET_SET
)) {
4136 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "offset", "clock class");
4141 ret
= get_unary_unsigned(ctx
, &entry_node
->u
.ctf_expression
.right
, offset_cycles
);
4143 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4144 entry_node
, "Unexpected unary expression for clock class's `offset` attribute.");
4149 _SET(set
, _CLOCK_OFFSET_SET
);
4150 } else if (strcmp(left
, "absolute") == 0) {
4151 struct ctf_node
*right
;
4153 if (_IS_SET(set
, _CLOCK_ABSOLUTE_SET
)) {
4154 _BT_CPPLOGE_APPEND_CAUSE_DUP_ATTR(entry_node
, "absolute", "clock class");
4160 _BT_LIST_FIRST_ENTRY(&entry_node
->u
.ctf_expression
.right
, struct ctf_node
, siblings
);
4161 ret
= get_boolean(ctx
, right
);
4163 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4164 entry_node
, "Unexpected unary expression for clock class's `absolute` attribute.");
4169 clock
->is_absolute
= ret
;
4170 _SET(set
, _CLOCK_ABSOLUTE_SET
);
4172 _BT_CPPLOGW_NODE(entry_node
, "Unknown attribute in clock class: attr-name=\"{}\"", left
);
4184 static inline uint64_t cycles_from_ns(uint64_t frequency
, uint64_t ns
)
4189 if (frequency
== UINT64_C(1000000000)) {
4192 cycles
= (uint64_t) (((double) ns
* (double) frequency
) / 1e9
);
4198 static void calibrate_clock_class_offsets(int64_t *offset_seconds
, uint64_t *offset_cycles
,
4201 if (*offset_cycles
>= freq
) {
4202 const uint64_t s_in_offset_cycles
= *offset_cycles
/ freq
;
4204 *offset_seconds
+= (int64_t) s_in_offset_cycles
;
4205 *offset_cycles
-= (s_in_offset_cycles
* freq
);
4209 static void apply_clock_class_is_absolute(struct ctf_visitor_generate_ir
*ctx
,
4210 struct ctf_clock_class
*clock
)
4212 if (ctx
->decoder_config
.force_clock_class_origin_unix_epoch
) {
4213 clock
->is_absolute
= true;
4219 static void apply_clock_class_offset(struct ctf_visitor_generate_ir
*ctx
,
4220 struct ctf_clock_class
*clock
)
4223 int64_t offset_s_to_apply
= ctx
->decoder_config
.clock_class_offset_s
;
4224 uint64_t offset_ns_to_apply
;
4225 int64_t cur_offset_s
;
4226 uint64_t cur_offset_cycles
;
4228 if (ctx
->decoder_config
.clock_class_offset_s
== 0 &&
4229 ctx
->decoder_config
.clock_class_offset_ns
== 0) {
4233 /* Transfer nanoseconds to seconds as much as possible */
4234 if (ctx
->decoder_config
.clock_class_offset_ns
< 0) {
4235 const int64_t abs_ns
= -ctx
->decoder_config
.clock_class_offset_ns
;
4236 const int64_t abs_extra_s
= abs_ns
/ INT64_C(1000000000) + 1;
4237 const int64_t extra_s
= -abs_extra_s
;
4238 const int64_t offset_ns
=
4239 ctx
->decoder_config
.clock_class_offset_ns
- (extra_s
* INT64_C(1000000000));
4241 BT_ASSERT(offset_ns
> 0);
4242 offset_ns_to_apply
= (uint64_t) offset_ns
;
4243 offset_s_to_apply
+= extra_s
;
4245 const int64_t extra_s
= ctx
->decoder_config
.clock_class_offset_ns
/ INT64_C(1000000000);
4246 const int64_t offset_ns
=
4247 ctx
->decoder_config
.clock_class_offset_ns
- (extra_s
* INT64_C(1000000000));
4249 BT_ASSERT(offset_ns
>= 0);
4250 offset_ns_to_apply
= (uint64_t) offset_ns
;
4251 offset_s_to_apply
+= extra_s
;
4254 freq
= clock
->frequency
;
4255 cur_offset_s
= clock
->offset_seconds
;
4256 cur_offset_cycles
= clock
->offset_cycles
;
4259 cur_offset_s
+= offset_s_to_apply
;
4260 cur_offset_cycles
+= cycles_from_ns(freq
, offset_ns_to_apply
);
4263 * Recalibrate offsets because the part in cycles can be greater
4264 * than the frequency at this point.
4266 calibrate_clock_class_offsets(&cur_offset_s
, &cur_offset_cycles
, freq
);
4268 /* Set final offsets */
4269 clock
->offset_seconds
= cur_offset_s
;
4270 clock
->offset_cycles
= cur_offset_cycles
;
4276 static int visit_clock_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*clock_node
)
4280 struct ctf_clock_class
*clock
;
4281 struct ctf_node
*entry_node
;
4282 struct bt_list_head
*decl_list
= &clock_node
->u
.clock
.declaration_list
;
4283 const char *clock_class_name
;
4284 int64_t offset_seconds
= 0;
4285 uint64_t offset_cycles
= 0;
4288 if (clock_node
->visited
) {
4292 clock_node
->visited
= TRUE
;
4294 /* CTF 1.8's default frequency for a clock class is 1 GHz */
4295 clock
= ctf_clock_class_create();
4297 _BT_CPPLOGE_APPEND_CAUSE_NODE(clock_node
, "Cannot create default clock class.");
4302 bt_list_for_each_entry (entry_node
, decl_list
, siblings
) {
4303 ret
= visit_clock_decl_entry(ctx
, entry_node
, clock
, &set
, &offset_seconds
, &offset_cycles
);
4305 _BT_CPPLOGE_APPEND_CAUSE_NODE(entry_node
, "Cannot visit clock class's entry: ret={}",
4311 if (!_IS_SET(&set
, _CLOCK_NAME_SET
)) {
4312 _BT_CPPLOGE_APPEND_CAUSE_NODE(clock_node
, "Missing `name` attribute in clock class.");
4317 clock_class_name
= clock
->name
->str
;
4318 BT_ASSERT(clock_class_name
);
4319 if (ctx
->is_lttng
&& strcmp(clock_class_name
, "monotonic") == 0) {
4321 * Old versions of LTTng forgot to set its clock class
4322 * as absolute, even if it is. This is important because
4323 * it's a condition to be able to sort messages
4324 * from different sources.
4326 clock
->is_absolute
= true;
4330 * Adjust offsets so that the part in cycles is less than the
4331 * frequency (move to the part in seconds).
4333 freq
= clock
->frequency
;
4334 calibrate_clock_class_offsets(&offset_seconds
, &offset_cycles
, freq
);
4335 BT_ASSERT(offset_cycles
< clock
->frequency
);
4336 clock
->offset_seconds
= offset_seconds
;
4337 clock
->offset_cycles
= offset_cycles
;
4338 apply_clock_class_offset(ctx
, clock
);
4339 apply_clock_class_is_absolute(ctx
, clock
);
4340 g_ptr_array_add(ctx
->ctf_tc
->clock_classes
, clock
);
4345 ctf_clock_class_destroy(clock
);
4351 static int visit_root_decl(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*root_decl_node
)
4355 if (root_decl_node
->visited
) {
4359 root_decl_node
->visited
= TRUE
;
4361 switch (root_decl_node
->type
) {
4364 visit_field_class_def(ctx
, root_decl_node
->u
.field_class_def
.field_class_specifier_list
,
4365 &root_decl_node
->u
.field_class_def
.field_class_declarators
);
4367 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
,
4368 "Cannot add field class found in root scope.");
4372 case NODE_TYPEALIAS
:
4373 ret
= visit_field_class_alias(ctx
, root_decl_node
->u
.field_class_alias
.target
,
4374 root_decl_node
->u
.field_class_alias
.alias
);
4376 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
,
4377 "Cannot add field class alias found in root scope.");
4381 case NODE_TYPE_SPECIFIER_LIST
:
4383 struct ctf_field_class
*decl
= NULL
;
4386 * Just add the field class specifier to the root
4387 * declaration scope. Put local reference.
4389 ret
= visit_field_class_specifier_list(ctx
, root_decl_node
, &decl
);
4391 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
,
4392 "Cannot visit root scope's field class: "
4399 ctf_field_class_destroy(decl
);
4404 _BT_CPPLOGE_APPEND_CAUSE_NODE(root_decl_node
, "Unexpected node type: node-type={}",
4405 root_decl_node
->type
);
4414 struct ctf_visitor_generate_ir
*
4415 ctf_visitor_generate_ir_create(const struct ctf_metadata_decoder_config
*decoder_config
)
4417 struct ctf_visitor_generate_ir
*ctx
= NULL
;
4419 bt2c::Logger logger
{decoder_config
->logger
, "PLUGIN/CTF/META/IR-VISITOR"};
4421 /* Create visitor's context */
4422 ctx
= ctx_create(decoder_config
, logger
);
4424 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot create visitor's context.");
4438 void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir
*visitor
)
4440 ctx_destroy(visitor
);
4443 bt_trace_class
*ctf_visitor_generate_ir_get_ir_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4447 if (ctx
->trace_class
) {
4448 bt_trace_class_get_ref(ctx
->trace_class
);
4451 return ctx
->trace_class
;
4454 struct ctf_trace_class
*
4455 ctf_visitor_generate_ir_borrow_ctf_trace_class(struct ctf_visitor_generate_ir
*ctx
)
4458 BT_ASSERT_DBG(ctx
->ctf_tc
);
4462 int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir
*ctx
, struct ctf_node
*node
)
4466 BT_CPPLOGI_STR_SPEC(ctx
->logger
, "Visiting metadata's AST to generate CTF IR objects.");
4468 switch (node
->type
) {
4471 struct ctf_node
*iter
;
4472 bool got_trace_decl
= false;
4475 * The first thing we need is the native byte order of
4476 * the trace block, because early class aliases can have
4477 * a `byte_order` attribute set to `native`. If we don't
4478 * have the native byte order yet, and we don't have any
4479 * trace block yet, then fail with EINCOMPLETE.
4481 if (ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_UNKNOWN
) {
4482 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4483 if (got_trace_decl
) {
4484 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Duplicate trace (`trace` block).");
4489 ret
= set_trace_byte_order(ctx
, iter
);
4491 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
,
4492 "Cannot set trace's native byte order: "
4498 got_trace_decl
= true;
4501 if (!got_trace_decl
) {
4502 BT_CPPLOGD_STR_SPEC(ctx
->logger
, "Incomplete AST: need trace (`trace` block).");
4508 BT_ASSERT(ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_LITTLE
||
4509 ctx
->ctf_tc
->default_byte_order
== CTF_BYTE_ORDER_BIG
);
4510 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4513 bt_list_for_each_entry (iter
, &node
->u
.root
.env
, siblings
) {
4514 ret
= visit_env(ctx
, iter
);
4516 _BT_CPPLOGE_APPEND_CAUSE_NODE(
4518 "Cannot visit trace's environment (`env` block) entry: "
4525 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4528 * Visit clock blocks.
4530 bt_list_for_each_entry (iter
, &node
->u
.root
.clock
, siblings
) {
4531 ret
= visit_clock_decl(ctx
, iter
);
4533 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit clock class: ret={}", ret
);
4538 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4541 * Visit root declarations next, as they can be used by any
4544 bt_list_for_each_entry (iter
, &node
->u
.root
.declaration_list
, siblings
) {
4545 ret
= visit_root_decl(ctx
, iter
);
4547 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit root entry: ret={}", ret
);
4552 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4554 /* Callsite blocks are not supported */
4555 bt_list_for_each_entry (iter
, &node
->u
.root
.callsite
, siblings
) {
4556 _BT_CPPLOGW_NODE(iter
, "\"callsite\" blocks are not supported as of this version.");
4559 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4562 bt_list_for_each_entry (iter
, &node
->u
.root
.trace
, siblings
) {
4563 ret
= visit_trace_decl(ctx
, iter
);
4565 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
,
4566 "Cannot visit trace (`trace` block): "
4573 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4576 bt_list_for_each_entry (iter
, &node
->u
.root
.stream
, siblings
) {
4577 ret
= visit_stream_decl(ctx
, iter
);
4579 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit stream class: ret={}", ret
);
4584 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4587 bt_list_for_each_entry (iter
, &node
->u
.root
.event
, siblings
) {
4588 ret
= visit_event_decl(ctx
, iter
);
4590 _BT_CPPLOGE_APPEND_CAUSE_NODE(iter
, "Cannot visit event class: ret={}", ret
);
4595 BT_ASSERT(ctx
->current_scope
&& !ctx
->current_scope
->parent_scope
);
4599 _BT_CPPLOGE_APPEND_CAUSE_NODE(node
, "Unexpected node type: node-type={}", node
->type
);
4604 /* Update default clock classes */
4605 ret
= ctf_trace_class_update_default_clock_classes(ctx
->ctf_tc
, ctx
->logger
);
4611 /* Update trace class meanings */
4612 ret
= ctf_trace_class_update_meanings(ctx
->ctf_tc
);
4618 /* Update stream class configuration */
4619 ret
= ctf_trace_class_update_stream_class_config(ctx
->ctf_tc
);
4625 /* Update text arrays and sequences */
4626 ret
= ctf_trace_class_update_text_array_sequence(ctx
->ctf_tc
);
4632 /* Update structure/array/sequence alignments */
4633 ret
= ctf_trace_class_update_alignments(ctx
->ctf_tc
);
4639 /* Resolve sequence lengths and variant tags */
4640 ret
= ctf_trace_class_resolve_field_classes(ctx
->ctf_tc
, ctx
->logger
);
4646 if (ctx
->trace_class
) {
4648 * Update "in IR" for field classes.
4650 * If we have no IR trace class, then we'll have no way
4651 * to create IR fields anyway, so we leave all the
4652 * `in_ir` members false.
4654 ret
= ctf_trace_class_update_in_ir(ctx
->ctf_tc
);
4661 /* Update saved value indexes */
4662 ret
= ctf_trace_class_update_value_storing_indexes(ctx
->ctf_tc
);
4668 /* Validate what we have so far */
4669 ret
= ctf_trace_class_validate(ctx
->ctf_tc
, ctx
->logger
);
4676 * If there are fields which are not related to the CTF format
4677 * itself in the packet header and in event header field
4678 * classes, warn about it because they are never translated.
4680 ctf_trace_class_warn_meaningless_header_fields(ctx
->ctf_tc
, ctx
->logger
);
4682 if (ctx
->trace_class
) {
4683 /* Copy new CTF metadata -> new IR metadata */
4685 ctf_trace_class_translate(ctx
->decoder_config
.self_comp
, ctx
->trace_class
, ctx
->ctf_tc
);