bt_ctf_field_type_get_byte_order(): support enum FT
[babeltrace.git] / formats / ctf / ir / field-types.c
index 1613dd19178ea2f9e7e19d4085982b2a3d62753d..c9c424ea76a3e901c80453a06a780ab919f952f6 100644 (file)
@@ -31,6 +31,7 @@
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/ref.h>
 #include <babeltrace/ctf-ir/clock.h>
+#include <babeltrace/ctf-ir/clock-internal.h>
 #include <babeltrace/ctf-writer/writer-internal.h>
 #include <babeltrace/object-internal.h>
 #include <babeltrace/ref.h>
@@ -90,6 +91,8 @@ void (* const type_destroy_funcs[])(struct bt_ctf_field_type *) = {
 static
 void generic_field_type_freeze(struct bt_ctf_field_type *);
 static
+void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *);
+static
 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *);
 static
 void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *);
@@ -102,7 +105,7 @@ void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *);
 
 static
 type_freeze_func const type_freeze_funcs[] = {
-       [BT_CTF_TYPE_ID_INTEGER] = generic_field_type_freeze,
+       [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_freeze,
        [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_freeze,
        [BT_CTF_TYPE_ID_FLOAT] = generic_field_type_freeze,
        [BT_CTF_TYPE_ID_STRUCT] = bt_ctf_field_type_structure_freeze,
@@ -267,6 +270,8 @@ int (* const type_compare_funcs[])(struct bt_ctf_field_type *,
        [BT_CTF_TYPE_ID_STRING] = bt_ctf_field_type_string_compare,
 };
 
+static
+int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *);
 static
 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *);
 static
@@ -280,7 +285,7 @@ int bt_ctf_field_type_sequence_validate(struct bt_ctf_field_type *);
 
 static
 int (* const type_validate_funcs[])(struct bt_ctf_field_type *) = {
-       [BT_CTF_TYPE_ID_INTEGER] = NULL,
+       [BT_CTF_TYPE_ID_INTEGER] = bt_ctf_field_type_integer_validate,
        [BT_CTF_TYPE_ID_FLOAT] = NULL,
        [BT_CTF_TYPE_ID_STRING] = NULL,
        [BT_CTF_TYPE_ID_ENUM] = bt_ctf_field_type_enumeration_validate,
@@ -399,8 +404,8 @@ int add_structure_field(GPtrArray *fields,
        field->name = name_quark;
        field->type = field_type;
        g_hash_table_insert(field_name_to_index,
-               (gpointer) (unsigned long) name_quark,
-               (gpointer) (unsigned long) fields->len);
+               GUINT_TO_POINTER(name_quark),
+               GUINT_TO_POINTER(fields->len));
        g_ptr_array_add(fields, field);
 end:
        return ret;
@@ -422,6 +427,24 @@ void bt_ctf_field_type_destroy(struct bt_object *obj)
        type_destroy_funcs[type_id](type);
 }
 
+static
+int bt_ctf_field_type_integer_validate(struct bt_ctf_field_type *type)
+{
+       int ret = 0;
+
+       struct bt_ctf_field_type_integer *integer =
+               container_of(type, struct bt_ctf_field_type_integer,
+                       parent);
+
+       if (integer->mapped_clock && integer->declaration.signedness) {
+               ret = -1;
+               goto end;
+       }
+
+end:
+       return ret;
+}
+
 static
 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type)
 {
@@ -821,7 +844,7 @@ int bt_ctf_field_type_integer_set_mapped_clock(
        struct bt_ctf_field_type_integer *integer;
        int ret = 0;
 
-       if (!type || type->frozen) {
+       if (!type || type->frozen || !bt_ctf_clock_is_valid(clock)) {
                ret = -1;
                goto end;
        }
@@ -1387,9 +1410,14 @@ int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type,
        int ret = 0;
        struct bt_ctf_field_type_structure *structure;
 
+       /*
+        * TODO: check that `field_type` does not contain `type`,
+        *       recursively.
+        */
        if (!type || !field_type || type->frozen ||
                bt_ctf_validate_identifier(field_name) ||
-               (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) {
+               (type->declaration->id != BT_CTF_TYPE_ID_STRUCT) ||
+               type == field_type) {
                ret = -1;
                goto end;
        }
@@ -1592,9 +1620,14 @@ int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type,
        struct bt_ctf_field_type_variant *variant;
        GQuark field_name_quark = g_quark_from_string(field_name);
 
+       /*
+        * TODO: check that `field_type` does not contain `type`,
+        *       recursively.
+        */
        if (!type || !field_type || type->frozen ||
                bt_ctf_validate_identifier(field_name) ||
-               (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) {
+               (type->declaration->id != BT_CTF_TYPE_ID_VARIANT) ||
+               type == field_type) {
                ret = -1;
                goto end;
        }
@@ -2117,6 +2150,13 @@ enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order(
                ret = integer->user_byte_order;
                break;
        }
+       case BT_CTF_TYPE_ID_ENUM:
+       {
+               struct bt_ctf_field_type_enumeration *enum_ft = container_of(
+                       type, struct bt_ctf_field_type_enumeration, parent);
+               ret = bt_ctf_field_type_get_byte_order(enum_ft->container);
+               break;
+       }
        case BT_CTF_TYPE_ID_FLOAT:
        {
                struct bt_ctf_field_type_floating_point *floating_point =
@@ -2349,7 +2389,6 @@ void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type,
        }
 }
 
-BT_HIDDEN
 struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type)
 {
        struct bt_ctf_field_type *copy = NULL;
@@ -2680,6 +2719,19 @@ void generic_field_type_freeze(struct bt_ctf_field_type *type)
        type->frozen = 1;
 }
 
+static
+void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_integer *integer_type = container_of(
+               type, struct bt_ctf_field_type_integer, parent);
+
+       if (integer_type->mapped_clock) {
+               bt_ctf_clock_freeze(integer_type->mapped_clock);
+       }
+
+       generic_field_type_freeze(type);
+}
+
 static
 void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type)
 {
@@ -4027,9 +4079,15 @@ struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
                        index);
                break;
        case CTF_TYPE_VARIANT:
-               bt_ctf_field_type_variant_get_field(field_type, NULL,
+       {
+               int ret = bt_ctf_field_type_variant_get_field(field_type, NULL,
                        &field, index);
+               if (ret) {
+                       field = NULL;
+                       goto end;
+               }
                break;
+       }
        case CTF_TYPE_ARRAY:
                field = bt_ctf_field_type_array_get_element_type(field_type);
                break;
@@ -4039,7 +4097,7 @@ struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
        default:
                break;
        }
-
+end:
        return field;
 }
 
This page took 0.02876 seconds and 4 git commands to generate.