Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / ctf / common / metadata / ctf-meta-translate.c
index e05fdba824b6ed2985991309adb7382f81af19d0..9c9e23eeebc91f04def1e08291ff87011e35c890 100644 (file)
@@ -1,24 +1,14 @@
 /*
- * Copyright 2018 - Philippe Proulx <pproulx@efficios.com>
+ * SPDX-License-Identifier: MIT
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
  */
 
-#define BT_LOG_TAG "PLUGIN/CTF/META/TRANSLATE"
-#include "logging.h"
-
 #include <babeltrace2/babeltrace.h>
 #include "common/macros.h"
 #include "common/assert.h"
 #include <glib.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <string.h>
 #include <inttypes.h>
@@ -26,7 +16,7 @@
 #include "ctf-meta-visitors.h"
 
 struct ctx {
-       bt_self_component_source *self_comp;
+       bt_self_component *self_comp;
        bt_trace_class *ir_tc;
        bt_stream_class *ir_sc;
        struct ctf_trace_class *tc;
@@ -56,9 +46,9 @@ bt_field_class *ctf_field_class_int_to_ir(struct ctx *ctx,
        bt_field_class *ir_fc;
 
        if (fc->is_signed) {
-               ir_fc = bt_field_class_signed_integer_create(ctx->ir_tc);
+               ir_fc = bt_field_class_integer_signed_create(ctx->ir_tc);
        } else {
-               ir_fc = bt_field_class_unsigned_integer_create(ctx->ir_tc);
+               ir_fc = bt_field_class_integer_unsigned_create(ctx->ir_tc);
        }
 
        BT_ASSERT(ir_fc);
@@ -75,9 +65,9 @@ bt_field_class *ctf_field_class_enum_to_ir(struct ctx *ctx,
        uint64_t i;
 
        if (fc->base.is_signed) {
-               ir_fc = bt_field_class_signed_enumeration_create(ctx->ir_tc);
+               ir_fc = bt_field_class_enumeration_signed_create(ctx->ir_tc);
        } else {
-               ir_fc = bt_field_class_unsigned_enumeration_create(ctx->ir_tc);
+               ir_fc = bt_field_class_enumeration_unsigned_create(ctx->ir_tc);
        }
 
        BT_ASSERT(ir_fc);
@@ -86,15 +76,43 @@ bt_field_class *ctf_field_class_enum_to_ir(struct ctx *ctx,
        for (i = 0; i < fc->mappings->len; i++) {
                struct ctf_field_class_enum_mapping *mapping =
                        ctf_field_class_enum_borrow_mapping_by_index(fc, i);
+               void *range_set;
+               uint64_t range_i;
+
+               if (fc->base.is_signed) {
+                       range_set = bt_integer_range_set_signed_create();
+               } else {
+                       range_set = bt_integer_range_set_unsigned_create();
+               }
+
+               BT_ASSERT(range_set);
+
+               for (range_i = 0; range_i < mapping->ranges->len; range_i++) {
+                       struct ctf_range *range =
+                               ctf_field_class_enum_mapping_borrow_range_by_index(
+                                       mapping, range_i);
+
+                       if (fc->base.is_signed) {
+                               ret = bt_integer_range_set_signed_add_range(
+                                       range_set, range->lower.i,
+                                       range->upper.i);
+                       } else {
+                               ret = bt_integer_range_set_unsigned_add_range(
+                                       range_set, range->lower.u,
+                                       range->upper.u);
+                       }
+
+                       BT_ASSERT(ret == 0);
+               }
 
                if (fc->base.is_signed) {
-                       ret = bt_field_class_signed_enumeration_map_range(
-                               ir_fc, mapping->label->str,
-                               mapping->range.lower.i, mapping->range.upper.i);
+                       ret = bt_field_class_enumeration_signed_add_mapping(
+                               ir_fc, mapping->label->str, range_set);
+                       BT_INTEGER_RANGE_SET_SIGNED_PUT_REF_AND_RESET(range_set);
                } else {
-                       ret = bt_field_class_unsigned_enumeration_map_range(
-                               ir_fc, mapping->label->str,
-                               mapping->range.lower.u, mapping->range.upper.u);
+                       ret = bt_field_class_enumeration_unsigned_add_mapping(
+                               ir_fc, mapping->label->str, range_set);
+                       BT_INTEGER_RANGE_SET_UNSIGNED_PUT_REF_AND_RESET(range_set);
                }
 
                BT_ASSERT(ret == 0);
@@ -109,13 +127,12 @@ bt_field_class *ctf_field_class_float_to_ir(struct ctx *ctx,
 {
        bt_field_class *ir_fc;
 
-       ir_fc = bt_field_class_real_create(ctx->ir_tc);
-       BT_ASSERT(ir_fc);
-
        if (fc->base.size == 32) {
-               bt_field_class_real_set_is_single_precision(ir_fc,
-                       BT_TRUE);
+               ir_fc = bt_field_class_real_single_precision_create(ctx->ir_tc);
+       } else {
+               ir_fc = bt_field_class_real_double_precision_create(ctx->ir_tc);
        }
+       BT_ASSERT(ir_fc);
 
        return ir_fc;
 }
@@ -186,24 +203,65 @@ bt_field_class *borrow_ir_fc_from_field_path(struct ctx *ctx,
        return ir_fc;
 }
 
+static inline
+const void *find_ir_enum_field_class_mapping_by_label(const bt_field_class *fc,
+               const char *label, bool is_signed)
+{
+       const void *mapping = NULL;
+       uint64_t i;
+
+       for (i = 0; i < bt_field_class_enumeration_get_mapping_count(fc); i++) {
+               const bt_field_class_enumeration_mapping *this_mapping;
+               const void *spec_this_mapping;
+
+               if (is_signed) {
+                       spec_this_mapping =
+                               bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
+                                       fc, i);
+                       this_mapping =
+                               bt_field_class_enumeration_signed_mapping_as_mapping_const(
+                                       spec_this_mapping);
+               } else {
+                       spec_this_mapping =
+                               bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
+                                       fc, i);
+                       this_mapping =
+                               bt_field_class_enumeration_unsigned_mapping_as_mapping_const(
+                                       spec_this_mapping);
+               }
+
+               BT_ASSERT(this_mapping);
+               BT_ASSERT(spec_this_mapping);
+
+               if (strcmp(bt_field_class_enumeration_mapping_get_label(
+                               this_mapping), label) == 0) {
+                       mapping = spec_this_mapping;
+                       goto end;
+               }
+       }
+
+end:
+       return mapping;
+}
+
 static inline
 bt_field_class *ctf_field_class_variant_to_ir(struct ctx *ctx,
                struct ctf_field_class_variant *fc)
 {
        int ret;
-       bt_field_class *ir_fc = bt_field_class_variant_create(ctx->ir_tc);
+       bt_field_class *ir_fc;
        uint64_t i;
-
-       BT_ASSERT(ir_fc);
+       bt_field_class *ir_tag_fc = NULL;
 
        if (fc->tag_path.root != CTF_SCOPE_PACKET_HEADER &&
                        fc->tag_path.root != CTF_SCOPE_EVENT_HEADER) {
-               ret = bt_field_class_variant_set_selector_field_class(
-                       ir_fc, borrow_ir_fc_from_field_path(ctx,
-                               &fc->tag_path));
-               BT_ASSERT(ret == 0);
+               ir_tag_fc = borrow_ir_fc_from_field_path(ctx, &fc->tag_path);
+               BT_ASSERT(ir_tag_fc);
        }
 
+       ir_fc = bt_field_class_variant_create(ctx->ir_tc, ir_tag_fc);
+       BT_ASSERT(ir_fc);
+
        for (i = 0; i < fc->options->len; i++) {
                struct ctf_named_field_class *named_fc =
                        ctf_field_class_variant_borrow_option_by_index(fc, i);
@@ -212,8 +270,56 @@ bt_field_class *ctf_field_class_variant_to_ir(struct ctx *ctx,
                BT_ASSERT(named_fc->fc->in_ir);
                option_ir_fc = ctf_field_class_to_ir(ctx, named_fc->fc);
                BT_ASSERT(option_ir_fc);
-               ret = bt_field_class_variant_append_option(
-                       ir_fc, named_fc->name->str, option_ir_fc);
+
+               if (ir_tag_fc) {
+                       /*
+                        * At this point the trace IR selector
+                        * (enumeration) field class already exists if
+                        * the variant is tagged (`ir_tag_fc`). This one
+                        * already contains range sets for its mappings,
+                        * so we just reuse the same, finding them by
+                        * matching a variant field class's option's
+                        * _original_ name (with a leading underscore,
+                        * possibly) with a selector field class's
+                        * mapping name.
+                        */
+                       if (fc->tag_fc->base.is_signed) {
+                               const bt_field_class_enumeration_signed_mapping *mapping =
+                                       find_ir_enum_field_class_mapping_by_label(
+                                               ir_tag_fc,
+                                               named_fc->orig_name->str, true);
+                               const bt_integer_range_set_signed *range_set;
+
+                               BT_ASSERT(mapping);
+                               range_set =
+                                       bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
+                                               mapping);
+                               BT_ASSERT(range_set);
+                               ret = bt_field_class_variant_with_selector_field_integer_signed_append_option(
+                                       ir_fc, named_fc->name->str,
+                                       option_ir_fc, range_set);
+                       } else {
+                               const bt_field_class_enumeration_unsigned_mapping *mapping =
+                                       find_ir_enum_field_class_mapping_by_label(
+                                               ir_tag_fc,
+                                               named_fc->orig_name->str,
+                                               false);
+                               const bt_integer_range_set_unsigned *range_set;
+
+                               BT_ASSERT(mapping);
+                               range_set =
+                                       bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
+                                               mapping);
+                               BT_ASSERT(range_set);
+                               ret = bt_field_class_variant_with_selector_field_integer_unsigned_append_option(
+                                       ir_fc, named_fc->name->str,
+                                       option_ir_fc, range_set);
+                       }
+               } else {
+                       ret = bt_field_class_variant_without_selector_append_option(
+                               ir_fc, named_fc->name->str, option_ir_fc);
+               }
+
                BT_ASSERT(ret == 0);
                bt_field_class_put_ref(option_ir_fc);
        }
@@ -236,7 +342,7 @@ bt_field_class *ctf_field_class_array_to_ir(struct ctx *ctx,
 
        elem_ir_fc = ctf_field_class_to_ir(ctx, fc->base.elem_fc);
        BT_ASSERT(elem_ir_fc);
-       ir_fc = bt_field_class_static_array_create(ctx->ir_tc, elem_ir_fc,
+       ir_fc = bt_field_class_array_static_create(ctx->ir_tc, elem_ir_fc,
                fc->length);
        BT_ASSERT(ir_fc);
        bt_field_class_put_ref(elem_ir_fc);
@@ -249,9 +355,9 @@ static inline
 bt_field_class *ctf_field_class_sequence_to_ir(struct ctx *ctx,
                struct ctf_field_class_sequence *fc)
 {
-       int ret;
        bt_field_class *ir_fc;
        bt_field_class *elem_ir_fc;
+       bt_field_class *length_fc = NULL;
 
        if (fc->base.is_text) {
                ir_fc = bt_field_class_string_create(ctx->ir_tc);
@@ -261,18 +367,19 @@ bt_field_class *ctf_field_class_sequence_to_ir(struct ctx *ctx,
 
        elem_ir_fc = ctf_field_class_to_ir(ctx, fc->base.elem_fc);
        BT_ASSERT(elem_ir_fc);
-       ir_fc = bt_field_class_dynamic_array_create(ctx->ir_tc, elem_ir_fc);
-       BT_ASSERT(ir_fc);
-       bt_field_class_put_ref(elem_ir_fc);
-       BT_ASSERT(ir_fc);
 
        if (fc->length_path.root != CTF_SCOPE_PACKET_HEADER &&
                        fc->length_path.root != CTF_SCOPE_EVENT_HEADER) {
-               ret = bt_field_class_dynamic_array_set_length_field_class(
-                       ir_fc, borrow_ir_fc_from_field_path(ctx, &fc->length_path));
-               BT_ASSERT(ret == 0);
+               length_fc = borrow_ir_fc_from_field_path(ctx, &fc->length_path);
+               BT_ASSERT(length_fc);
        }
 
+       ir_fc = bt_field_class_array_dynamic_create(ctx->ir_tc, elem_ir_fc,
+               length_fc);
+       BT_ASSERT(ir_fc);
+       bt_field_class_put_ref(elem_ir_fc);
+       BT_ASSERT(ir_fc);
+
 end:
        return ir_fc;
 }
@@ -312,7 +419,7 @@ bt_field_class *ctf_field_class_to_ir(struct ctx *ctx,
                ir_fc = ctf_field_class_variant_to_ir(ctx, (void *) fc);
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        fc->ir_fc = ir_fc;
@@ -370,7 +477,7 @@ bt_field_class *scope_ctf_field_class_to_ir(struct ctx *ctx)
                fc = ctx->ec->payload_fc;
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        if (fc && ctf_field_class_struct_has_immediate_member_in_ir(
@@ -428,7 +535,7 @@ void ctf_event_class_to_ir(struct ctx *ctx)
                BT_ASSERT(ret == 0);
        }
 
-       if (ctx->ec->log_level != -1) {
+       if (ctx->ec->is_log_level_set) {
                bt_event_class_set_log_level(ir_ec, ctx->ec->log_level);
        }
 
@@ -458,6 +565,22 @@ void ctf_stream_class_to_ir(struct ctx *ctx)
        ctx->ir_sc = bt_stream_class_create_with_id(ctx->ir_tc, ctx->sc->id);
        BT_ASSERT(ctx->ir_sc);
        bt_stream_class_put_ref(ctx->ir_sc);
+
+       if (ctx->sc->default_clock_class) {
+               BT_ASSERT(ctx->sc->default_clock_class->ir_cc);
+               ret = bt_stream_class_set_default_clock_class(ctx->ir_sc,
+                       ctx->sc->default_clock_class->ir_cc);
+               BT_ASSERT(ret == 0);
+       }
+
+       bt_stream_class_set_supports_packets(ctx->ir_sc, BT_TRUE,
+               ctx->sc->packets_have_ts_begin, ctx->sc->packets_have_ts_end);
+       bt_stream_class_set_supports_discarded_events(ctx->ir_sc,
+               ctx->sc->has_discarded_events,
+               ctx->sc->discarded_events_have_default_cs);
+       bt_stream_class_set_supports_discarded_packets(ctx->ir_sc,
+               ctx->sc->has_discarded_packets,
+               ctx->sc->discarded_packets_have_default_cs);
        ctx->scope = CTF_SCOPE_PACKET_CONTEXT;
        ir_fc = scope_ctf_field_class_to_ir(ctx);
        if (ir_fc) {
@@ -480,23 +603,6 @@ void ctf_stream_class_to_ir(struct ctx *ctx)
                BT_FALSE);
        bt_stream_class_set_assigns_automatic_stream_id(ctx->ir_sc, BT_FALSE);
 
-       if (ctx->sc->default_clock_class) {
-               BT_ASSERT(ctx->sc->default_clock_class->ir_cc);
-               ret = bt_stream_class_set_default_clock_class(ctx->ir_sc,
-                       ctx->sc->default_clock_class->ir_cc);
-               BT_ASSERT(ret == 0);
-       }
-
-       bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
-               ctx->ir_sc, ctx->sc->packets_have_ts_begin);
-       bt_stream_class_set_packets_have_end_default_clock_snapshot(
-               ctx->ir_sc, ctx->sc->packets_have_ts_end);
-       bt_stream_class_set_supports_discarded_events(ctx->ir_sc,
-               ctx->sc->has_discarded_events,
-               ctx->sc->discarded_events_have_default_cs);
-       bt_stream_class_set_supports_discarded_packets(ctx->ir_sc,
-               ctx->sc->has_discarded_packets,
-               ctx->sc->discarded_packets_have_default_cs);
        ctx->sc->is_translated = true;
        ctx->sc->ir_sc = ctx->ir_sc;
 
@@ -543,40 +649,10 @@ int ctf_trace_class_to_ir(struct ctx *ctx)
                goto end;
        }
 
-       if (ctx->tc->is_uuid_set) {
-               bt_trace_class_set_uuid(ctx->ir_tc, ctx->tc->uuid);
-       }
-
-       for (i = 0; i < ctx->tc->env_entries->len; i++) {
-               struct ctf_trace_class_env_entry *env_entry =
-                       ctf_trace_class_borrow_env_entry_by_index(ctx->tc, i);
-
-               switch (env_entry->type) {
-               case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_INT:
-                       ret = bt_trace_class_set_environment_entry_integer(
-                               ctx->ir_tc, env_entry->name->str,
-                               env_entry->value.i);
-                       break;
-               case CTF_TRACE_CLASS_ENV_ENTRY_TYPE_STR:
-                       ret = bt_trace_class_set_environment_entry_string(
-                               ctx->ir_tc, env_entry->name->str,
-                               env_entry->value.str->str);
-                       break;
-               default:
-                       abort();
-               }
-
-               if (ret) {
-                       goto end;
-               }
-       }
-
        for (i = 0; i < ctx->tc->clock_classes->len; i++) {
                struct ctf_clock_class *cc = ctx->tc->clock_classes->pdata[i];
 
-               cc->ir_cc = bt_clock_class_create(
-                               bt_self_component_source_as_self_component(
-                                       ctx->self_comp));
+               cc->ir_cc = bt_clock_class_create(ctx->self_comp);
                ctf_clock_class_to_ir(cc->ir_cc, cc);
        }
 
@@ -590,7 +666,7 @@ end:
 }
 
 BT_HIDDEN
-int ctf_trace_class_translate(bt_self_component_source *self_comp,
+int ctf_trace_class_translate(bt_self_component *self_comp,
                bt_trace_class *ir_tc, struct ctf_trace_class *tc)
 {
        int ret = 0;
This page took 0.029484 seconds and 4 git commands to generate.