Make API CTF-agnostic
[babeltrace.git] / include / babeltrace / ctf-ir / field-types-internal.h
index dcb5b12ab3211963460b2b38752e86c8aaa9f3c2..fcefa0637c9ce019f9a88efe6c31756c410c13a2 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/event-types.h>
-#include <babeltrace/ctf-writer/event-fields.h>
-#include <babeltrace/ctf-writer/writer.h>
-#include <babeltrace/ctf-ir/trace-internal.h>
+#include <babeltrace/assert-pre-internal.h>
 #include <babeltrace/ctf-ir/clock-class.h>
+#include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/object-internal.h>
+#include <babeltrace/types.h>
+#include <stdint.h>
 #include <glib.h>
 
-typedef void (*type_freeze_func)(struct bt_ctf_field_type *);
-typedef int (*type_serialize_func)(struct bt_ctf_field_type *,
-               struct metadata_context *);
-
-struct bt_ctf_field_type {
+#define BT_ASSERT_PRE_FT_IS_INT(_ft, _name)                            \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_SIGNED_INTEGER || \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION || \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \
+               _name " is not an integer field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_IS_UNSIGNED_INT(_ft, _name)                   \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION, \
+               _name " is not an unsigned integer field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_IS_ENUM(_ft, _name)                           \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION || \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \
+               _name " is not an enumeration field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_IS_ARRAY(_ft, _name)                          \
+       BT_ASSERT_PRE(                                                  \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_STATIC_ARRAY || \
+               ((struct bt_field_type *) (_ft))->id == BT_FIELD_TYPE_ID_DYNAMIC_ARRAY, \
+               _name " is not an array field type: %![ft-]+F", (_ft))
+
+#define BT_ASSERT_PRE_FT_HAS_ID(_ft, _id, _name)                       \
+       BT_ASSERT_PRE(((struct bt_field_type *) (_ft))->id == (_id),    \
+               _name " has the wrong ID: expected-id=%s, "             \
+               "%![ft-]+F", bt_common_field_type_id_string(_id), (_ft))
+
+#define BT_ASSERT_PRE_FT_HOT(_ft, _name)                               \
+       BT_ASSERT_PRE_HOT((struct bt_field_type *) (_ft),               \
+               (_name), ": %!+F", (_ft))
+
+#define BT_FIELD_TYPE_NAMED_FT_AT_INDEX(_ft, _index)           \
+       (&g_array_index(((struct bt_field_type_named_field_types_container *) (_ft))->named_fts, \
+               struct bt_named_field_type, (_index)))
+
+#define BT_FIELD_TYPE_ENUM_MAPPING_AT_INDEX(_ft, _index)               \
+       (&g_array_index(((struct bt_field_type_enumeration *) (_ft))->mappings, \
+               struct bt_field_type_enumeration_mapping, (_index)))
+
+#define BT_FIELD_TYPE_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index)    \
+       (&g_array_index((_mapping)->ranges,                             \
+               struct bt_field_type_enumeration_mapping_range, (_index)))
+
+struct bt_field;
+struct bt_field_type;
+
+struct bt_field_type {
        struct bt_object base;
-       enum bt_ctf_type_id id;
-       unsigned int alignment;
-       type_freeze_func freeze;
-       type_serialize_func serialize;
+       enum bt_field_type_id id;
+       bool frozen;
+
        /*
-        * A type can't be modified once it is added to an event or after a
-        * a field has been instanciated from it.
+        * Only used in developer mode, this flag indicates whether or
+        * not this field type is part of a trace.
         */
-       int frozen;
+       bool part_of_trace;
+};
+
+struct bt_field_type_integer {
+       struct bt_field_type common;
 
        /*
-        * This flag indicates if the field type is valid. A valid
-        * field type is _always_ frozen. All the nested field types of
-        * a valid field type are also valid (and thus frozen).
+        * Value range of fields built from this integer field type:
+        * this is an equivalent integer size in bits. More formally,
+        * `range` is `n` in:
+        *
+        * Unsigned range: [0, 2^n - 1]
+        * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
         */
-       int valid;
-};
+       uint64_t range;
 
-struct bt_ctf_field_type_integer {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_clock_class *mapped_clock;
-       enum bt_ctf_byte_order user_byte_order;
-       bool is_signed;
-       unsigned int size;
-       enum bt_ctf_integer_base base;
-       enum bt_ctf_string_encoding encoding;
+       enum bt_field_type_integer_preferred_display_base base;
 };
 
-struct enumeration_mapping {
+struct bt_field_type_enumeration_mapping_range {
        union {
-               uint64_t _unsigned;
-               int64_t _signed;
-       } range_start;
+               uint64_t u;
+               int64_t i;
+       } lower;
 
        union {
-               uint64_t _unsigned;
-               int64_t _signed;
-       } range_end;
-       GQuark string;
+               uint64_t u;
+               int64_t i;
+       } upper;
 };
 
-struct bt_ctf_field_type_enumeration {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_field_type *container;
-       GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */
-       /* Only set during validation. */
-       bool has_overlapping_ranges;
-};
+struct bt_field_type_enumeration_mapping {
+       GString *label;
 
-enum bt_ctf_field_type_enumeration_mapping_iterator_type {
-       ITERATOR_BY_NAME,
-       ITERATOR_BY_SIGNED_VALUE,
-       ITERATOR_BY_UNSIGNED_VALUE,
+       /* Array of `struct bt_field_type_enumeration_mapping_range` */
+       GArray *ranges;
 };
 
-struct bt_ctf_field_type_enumeration_mapping_iterator {
-       struct bt_object base;
-       struct bt_ctf_field_type_enumeration *enumeration_type;
-       enum bt_ctf_field_type_enumeration_mapping_iterator_type type;
-       int index;
-       union {
-               GQuark name_quark;
-               int64_t signed_value;
-               uint64_t unsigned_value;
-       } u;
-};
+struct bt_field_type_enumeration {
+       struct bt_field_type_integer common;
 
-struct bt_ctf_field_type_floating_point {
-       struct bt_ctf_field_type parent;
-       enum bt_ctf_byte_order user_byte_order;
-       unsigned int exp_dig;
-       unsigned int mant_dig;
-};
+       /* Array of `struct bt_field_type_enumeration_mapping *` */
+       GArray *mappings;
 
-struct structure_field {
-       GQuark name;
-       struct bt_ctf_field_type *type;
+       /*
+        * This is an array of `const char *` which acts as a temporary
+        * (potentially growing) buffer for
+        * bt_field_type_unsigned_enumeration_get_mapping_labels_by_value()
+        * and
+        * bt_field_type_signed_enumeration_get_mapping_labels_by_value().
+        *
+        * The actual strings are owned by the mappings above.
+        */
+       GPtrArray *label_buf;
 };
 
-struct bt_ctf_field_type_structure {
-       struct bt_ctf_field_type parent;
-       GHashTable *field_name_to_index;
-       GPtrArray *fields; /* Array of pointers to struct structure_field */
+struct bt_field_type_real {
+       struct bt_field_type common;
+       bool is_single_precision;
 };
 
-struct bt_ctf_field_type_variant {
-       struct bt_ctf_field_type parent;
-       GString *tag_name;
-       struct bt_ctf_field_type_enumeration *tag;
-       struct bt_ctf_field_path *tag_field_path;
-       GHashTable *field_name_to_index;
-       GPtrArray *fields; /* Array of pointers to struct structure_field */
+struct bt_field_type_string {
+       struct bt_field_type common;
 };
 
-struct bt_ctf_field_type_array {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_field_type *element_type;
-       unsigned int length; /* Number of elements */
-};
+/* A named field type is a (name, field type) pair */
+struct bt_named_field_type {
+       GString *name;
 
-struct bt_ctf_field_type_sequence {
-       struct bt_ctf_field_type parent;
-       struct bt_ctf_field_type *element_type;
-       GString *length_field_name;
-       struct bt_ctf_field_path *length_field_path;
+       /* Owned by this */
+       struct bt_field_type *ft;
 };
 
-struct bt_ctf_field_type_string {
-       struct bt_ctf_field_type parent;
-       enum bt_ctf_string_encoding encoding;
-};
+/*
+ * This is the base field type for a container of named field types.
+ * Structure and variant field types inherit this.
+ */
+struct bt_field_type_named_field_types_container {
+       struct bt_field_type common;
 
-BT_HIDDEN
-void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type);
+       /*
+        * Key: `const char *`, not owned by this (owned by named field
+        * type objects contained in `named_fts` below).
+        */
+       GHashTable *name_to_index;
 
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
-               struct bt_ctf_field_type_variant *variant, int64_t tag_value);
+       /* Array of `struct bt_named_field_type` */
+       GArray *named_fts;
+};
 
-BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
-               struct bt_ctf_field_type_variant *variant, uint64_t tag_value);
+struct bt_field_type_structure {
+       struct bt_field_type_named_field_types_container common;
+};
 
-BT_HIDDEN
-int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
-               struct metadata_context *context);
+struct bt_field_type_array {
+       struct bt_field_type common;
 
-BT_HIDDEN
-int bt_ctf_field_type_validate(struct bt_ctf_field_type *type);
+       /* Owned by this */
+       struct bt_field_type *element_ft;
+};
 
-BT_HIDDEN
-int bt_ctf_field_type_structure_get_field_name_index(
-               struct bt_ctf_field_type *structure, const char *name);
+struct bt_field_type_static_array {
+       struct bt_field_type_array common;
+       uint64_t length;
+};
 
-/* Replace an existing field's type in a structure */
-BT_HIDDEN
-int bt_ctf_field_type_structure_set_field_index(
-               struct bt_ctf_field_type *structure,
-               struct bt_ctf_field_type *field, int index);
+struct bt_field_type_dynamic_array {
+       struct bt_field_type_array common;
 
-BT_HIDDEN
-int bt_ctf_field_type_variant_get_field_name_index(
-               struct bt_ctf_field_type *variant, const char *name);
+       /* Weak: never dereferenced, only use to find it elsewhere */
+       struct bt_field_type *length_ft;
 
-BT_HIDDEN
-int bt_ctf_field_type_sequence_set_length_field_path(
-               struct bt_ctf_field_type *type,
-               struct bt_ctf_field_path *path);
+       /* Owned by this */
+       struct bt_field_path *length_field_path;
+};
 
-BT_HIDDEN
-int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
-               struct bt_ctf_field_path *path);
+struct bt_field_type_variant {
+       struct bt_field_type_named_field_types_container common;
 
-BT_HIDDEN
-int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
-               struct bt_ctf_field_type *tag_type);
+       /* Weak: never dereferenced, only use to find it elsewhere */
+       struct bt_field_type *selector_ft;
 
-/* Replace an existing field's type in a variant */
-BT_HIDDEN
-int bt_ctf_field_type_variant_set_field_index(
-               struct bt_ctf_field_type *variant,
-               struct bt_ctf_field_type *field, int index);
+       /* Owned by this */
+       struct bt_field_path *selector_field_path;
+};
 
-BT_HIDDEN
-int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array,
-               struct bt_ctf_field_type *element_type);
+static inline
+bool bt_field_type_has_known_id(struct bt_field_type *ft)
+{
+       return ft->id >= BT_FIELD_TYPE_ID_UNSIGNED_INTEGER &&
+               ft->id <= BT_FIELD_TYPE_ID_VARIANT;
+}
 
 BT_HIDDEN
-int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array,
-               struct bt_ctf_field_type *element_type);
+void _bt_field_type_freeze(struct bt_field_type *field_type);
 
-BT_HIDDEN
-int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type);
+#ifdef BT_DEV_MODE
+# define bt_field_type_freeze          _bt_field_type_freeze
+#else
+# define bt_field_type_freeze(_ft)
+#endif
 
+/*
+ * This function recursively marks `field_type` and its children as
+ * being part of a trace. This is used to validate that all field types
+ * are used at a single location within trace objects even if they are
+ * shared objects for other purposes.
+ */
 BT_HIDDEN
-struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
-               struct bt_ctf_field_type *type, int index);
+void _bt_field_type_make_part_of_trace(struct bt_field_type *field_type);
 
-BT_HIDDEN
-int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type,
-               const char *name);
+#ifdef BT_DEV_MODE
+# define bt_field_type_make_part_of_trace      _bt_field_type_make_part_of_trace
+#else
+# define bt_field_type_make_part_of_trace(_ft) ((void) _ft)
+#endif
 
 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */
This page took 0.032239 seconds and 4 git commands to generate.