Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / trace-ir / resolve-field-path.c
index ed44e059e7dcba5c5284d79557770e22bcafbc1b..25a5fb687561901dc747ff6c174fa9de712ebd59 100644 (file)
@@ -1,23 +1,7 @@
 /*
- * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
- *
- * 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.
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2018 Philippe Proulx <pproulx@efficios.com>
  */
 
 #define BT_LOG_TAG "LIB/RESOLVE-FIELD-PATH"
@@ -25,8 +9,9 @@
 
 #include "lib/assert-pre.h"
 #include "common/assert.h"
-#include <babeltrace2/trace-ir/field-path-const.h>
+#include <babeltrace2/trace-ir/field-path.h>
 #include <limits.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <inttypes.h>
 #include <glib.h>
@@ -46,18 +31,31 @@ bool find_field_class_recursive(struct bt_field_class *fc,
                goto end;
        }
 
-       switch (fc->type) {
-       case BT_FIELD_CLASS_TYPE_STRUCTURE:
-       case BT_FIELD_CLASS_TYPE_VARIANT:
-       {
+       if (bt_field_class_type_is(fc->type, BT_FIELD_CLASS_TYPE_OPTION)) {
+               struct bt_field_class_option *opt_fc = (void *) fc;
+               struct bt_field_path_item item = {
+                       .type = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT,
+                       .index = UINT64_C(-1),
+               };
+
+               bt_field_path_append_item(field_path, &item);
+               found = find_field_class_recursive(opt_fc->content_fc,
+                       tgt_fc, field_path);
+               if (found) {
+                       goto end;
+               }
+
+               bt_field_path_remove_last_item(field_path);
+       } else if (fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE ||
+                       bt_field_class_type_is(fc->type,
+                               BT_FIELD_CLASS_TYPE_VARIANT)) {
                struct bt_field_class_named_field_class_container *container_fc =
                        (void *) fc;
                uint64_t i;
 
                for (i = 0; i < container_fc->named_fcs->len; i++) {
                        struct bt_named_field_class *named_fc =
-                               BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
-                                       container_fc, i);
+                               container_fc->named_fcs->pdata[i];
                        struct bt_field_path_item item = {
                                .type = BT_FIELD_PATH_ITEM_TYPE_INDEX,
                                .index = i,
@@ -72,12 +70,7 @@ bool find_field_class_recursive(struct bt_field_class *fc,
 
                        bt_field_path_remove_last_item(field_path);
                }
-
-               break;
-       }
-       case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
-       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
-       {
+       } else if (bt_field_class_type_is(fc->type, BT_FIELD_CLASS_TYPE_ARRAY)) {
                struct bt_field_class_array *array_fc = (void *) fc;
                struct bt_field_path_item item = {
                        .type = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT,
@@ -92,10 +85,6 @@ bool find_field_class_recursive(struct bt_field_class *fc,
                }
 
                bt_field_path_remove_last_item(field_path);
-               break;
-       }
-       default:
-               break;
        }
 
 end:
@@ -104,7 +93,7 @@ end:
 
 static
 int find_field_class(struct bt_field_class *root_fc,
-               enum bt_scope root_scope, struct bt_field_class *tgt_fc,
+               enum bt_field_path_scope root_scope, struct bt_field_class *tgt_fc,
                struct bt_field_path **ret_field_path)
 {
        int ret = 0;
@@ -138,25 +127,25 @@ struct bt_field_path *find_field_class_in_ctx(struct bt_field_class *fc,
        struct bt_field_path *field_path = NULL;
        int ret;
 
-       ret = find_field_class(ctx->packet_context, BT_SCOPE_PACKET_CONTEXT,
+       ret = find_field_class(ctx->packet_context, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT,
                fc, &field_path);
        if (ret || field_path) {
                goto end;
        }
 
        ret = find_field_class(ctx->event_common_context,
-               BT_SCOPE_EVENT_COMMON_CONTEXT, fc, &field_path);
+               BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT, fc, &field_path);
        if (ret || field_path) {
                goto end;
        }
 
        ret = find_field_class(ctx->event_specific_context,
-               BT_SCOPE_EVENT_SPECIFIC_CONTEXT, fc, &field_path);
+               BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT, fc, &field_path);
        if (ret || field_path) {
                goto end;
        }
 
-       ret = find_field_class(ctx->event_payload, BT_SCOPE_EVENT_PAYLOAD,
+       ret = find_field_class(ctx->event_payload, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD,
                fc, &field_path);
        if (ret || field_path) {
                goto end;
@@ -166,7 +155,7 @@ end:
        return field_path;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 bool target_is_before_source(struct bt_field_path *src_field_path,
                struct bt_field_path *tgt_field_path)
@@ -210,28 +199,28 @@ end:
        return is_valid;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 struct bt_field_class *borrow_root_field_class(
-               struct bt_resolve_field_path_context *ctx, enum bt_scope scope)
+               struct bt_resolve_field_path_context *ctx, enum bt_field_path_scope scope)
 {
        switch (scope) {
-       case BT_SCOPE_PACKET_CONTEXT:
+       case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT:
                return ctx->packet_context;
-       case BT_SCOPE_EVENT_COMMON_CONTEXT:
+       case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT:
                return ctx->event_common_context;
-       case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
+       case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT:
                return ctx->event_specific_context;
-       case BT_SCOPE_EVENT_PAYLOAD:
+       case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD:
                return ctx->event_payload;
        default:
-               abort();
+               bt_common_abort();
        }
 
        return NULL;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 struct bt_field_class *borrow_child_field_class(
                struct bt_field_class *parent_fc,
@@ -239,36 +228,36 @@ struct bt_field_class *borrow_child_field_class(
 {
        struct bt_field_class *child_fc = NULL;
 
-       switch (parent_fc->type) {
-       case BT_FIELD_CLASS_TYPE_STRUCTURE:
-       case BT_FIELD_CLASS_TYPE_VARIANT:
-       {
+       if (bt_field_class_type_is(parent_fc->type,
+                       BT_FIELD_CLASS_TYPE_OPTION)) {
+               struct bt_field_class_option *opt_fc = (void *) parent_fc;
+
+               BT_ASSERT(fp_item->type ==
+                       BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT);
+               child_fc = opt_fc->content_fc;
+       } else if (parent_fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE ||
+                       bt_field_class_type_is(parent_fc->type,
+                               BT_FIELD_CLASS_TYPE_VARIANT)) {
+               struct bt_field_class_named_field_class_container *container_fc =
+                       (void *) parent_fc;
                struct bt_named_field_class *named_fc;
 
                BT_ASSERT(fp_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX);
-               named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(parent_fc,
-                       fp_item->index);
+               named_fc = container_fc->named_fcs->pdata[fp_item->index];
                child_fc = named_fc->fc;
-               break;
-       }
-       case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
-       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
-       {
+       } else if (bt_field_class_type_is(parent_fc->type,
+                       BT_FIELD_CLASS_TYPE_ARRAY)) {
                struct bt_field_class_array *array_fc = (void *) parent_fc;
 
                BT_ASSERT(fp_item->type ==
                        BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT);
                child_fc = array_fc->element_fc;
-               break;
-       }
-       default:
-               break;
        }
 
        return child_fc;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 bool target_field_path_in_different_scope_has_struct_fc_only(
                struct bt_field_path *src_field_path,
@@ -290,9 +279,12 @@ bool target_field_path_in_different_scope_has_struct_fc_only(
                        bt_field_path_borrow_item_by_index_inline(
                                tgt_field_path, i);
 
-               if (fc->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY ||
-                               fc->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY ||
-                               fc->type == BT_FIELD_CLASS_TYPE_VARIANT) {
+               if (bt_field_class_type_is(fc->type,
+                               BT_FIELD_CLASS_TYPE_ARRAY) ||
+                               bt_field_class_type_is(fc->type,
+                                       BT_FIELD_CLASS_TYPE_OPTION) ||
+                               bt_field_class_type_is(fc->type,
+                                       BT_FIELD_CLASS_TYPE_VARIANT)) {
                        is_valid = false;
                        goto end;
                }
@@ -305,7 +297,7 @@ end:
        return is_valid;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 bool lca_is_structure_field_class(struct bt_field_path *src_field_path,
                struct bt_field_path *tgt_field_path,
@@ -361,7 +353,7 @@ end:
        return is_valid;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 bool lca_to_target_has_struct_fc_only(struct bt_field_path *src_field_path,
                struct bt_field_path *tgt_field_path,
@@ -407,9 +399,12 @@ bool lca_to_target_has_struct_fc_only(struct bt_field_path *src_field_path,
                        bt_field_path_borrow_item_by_index_inline(
                                tgt_field_path, tgt_i);
 
-               if (tgt_fc->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY ||
-                               tgt_fc->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY ||
-                               tgt_fc->type == BT_FIELD_CLASS_TYPE_VARIANT) {
+               if (bt_field_class_type_is(tgt_fc->type,
+                               BT_FIELD_CLASS_TYPE_ARRAY) ||
+                               bt_field_class_type_is(tgt_fc->type,
+                                       BT_FIELD_CLASS_TYPE_OPTION) ||
+                               bt_field_class_type_is(tgt_fc->type,
+                                       BT_FIELD_CLASS_TYPE_VARIANT)) {
                        is_valid = false;
                        goto end;
                }
@@ -421,7 +416,7 @@ end:
        return is_valid;
 }
 
-BT_ASSERT_PRE_FUNC
+BT_ASSERT_PRE_DEV_FUNC
 static inline
 bool field_path_is_valid(struct bt_field_class *src_fc,
                struct bt_field_class *tgt_fc,
@@ -434,14 +429,14 @@ bool field_path_is_valid(struct bt_field_class *src_fc,
                tgt_fc, ctx);
 
        if (!src_field_path) {
-               BT_ASSERT_PRE_MSG("Cannot find requesting field class in "
+               BT_ASSERT_PRE_DEV_MSG("Cannot find requesting field class in "
                        "resolving context: %!+F", src_fc);
                is_valid = false;
                goto end;
        }
 
        if (!tgt_field_path) {
-               BT_ASSERT_PRE_MSG("Cannot find target field class in "
+               BT_ASSERT_PRE_DEV_MSG("Cannot find target field class in "
                        "resolving context: %!+F", tgt_fc);
                is_valid = false;
                goto end;
@@ -449,7 +444,7 @@ bool field_path_is_valid(struct bt_field_class *src_fc,
 
        /* Target must be before source */
        if (!target_is_before_source(src_field_path, tgt_field_path)) {
-               BT_ASSERT_PRE_MSG("Target field class is located after "
+               BT_ASSERT_PRE_DEV_MSG("Target field class is located after "
                        "requesting field class: %![req-fc-]+F, %![tgt-fc-]+F",
                        src_fc, tgt_fc);
                is_valid = false;
@@ -462,7 +457,7 @@ bool field_path_is_valid(struct bt_field_class *src_fc,
         */
        if (!target_field_path_in_different_scope_has_struct_fc_only(
                        src_field_path, tgt_field_path, ctx)) {
-               BT_ASSERT_PRE_MSG("Target field class is located in a "
+               BT_ASSERT_PRE_DEV_MSG("Target field class is located in a "
                        "different scope than requesting field class, "
                        "but within an array or a variant field class: "
                        "%![req-fc-]+F, %![tgt-fc-]+F",
@@ -473,7 +468,7 @@ bool field_path_is_valid(struct bt_field_class *src_fc,
 
        /* Same scope: LCA must be a structure field class */
        if (!lca_is_structure_field_class(src_field_path, tgt_field_path, ctx)) {
-               BT_ASSERT_PRE_MSG("Lowest common ancestor of target and "
+               BT_ASSERT_PRE_DEV_MSG("Lowest common ancestor of target and "
                        "requesting field classes is not a structure field class: "
                        "%![req-fc-]+F, %![tgt-fc-]+F",
                        src_fc, tgt_fc);
@@ -484,7 +479,7 @@ bool field_path_is_valid(struct bt_field_class *src_fc,
        /* Same scope: path from LCA to target has no array/variant FTs */
        if (!lca_to_target_has_struct_fc_only(src_field_path, tgt_field_path,
                        ctx)) {
-               BT_ASSERT_PRE_MSG("Path from lowest common ancestor of target "
+               BT_ASSERT_PRE_DEV_MSG("Path from lowest common ancestor of target "
                        "and requesting field classes to target field class "
                        "contains an array or a variant field class: "
                        "%![req-fc-]+F, %![tgt-fc-]+F", src_fc, tgt_fc);
@@ -503,7 +498,7 @@ struct bt_field_path *resolve_field_path(struct bt_field_class *src_fc,
                struct bt_field_class *tgt_fc,
                struct bt_resolve_field_path_context *ctx)
 {
-       BT_ASSERT_PRE(field_path_is_valid(src_fc, tgt_fc, ctx),
+       BT_ASSERT_PRE_DEV(field_path_is_valid(src_fc, tgt_fc, ctx),
                "Invalid target field class: %![req-fc-]+F, %![tgt-fc-]+F",
                src_fc, tgt_fc);
        return find_field_class_in_ctx(tgt_fc, ctx);
@@ -518,74 +513,72 @@ int bt_resolve_field_paths(struct bt_field_class *fc,
        BT_ASSERT(fc);
 
        /* Resolving part for dynamic array and variant field classes */
-       switch (fc->type) {
-       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
-       {
-               struct bt_field_class_dynamic_array *dyn_array_fc = (void *) fc;
-
-               if (dyn_array_fc->length_fc) {
-                       BT_ASSERT(!dyn_array_fc->length_field_path);
-                       dyn_array_fc->length_field_path = resolve_field_path(
-                               fc, dyn_array_fc->length_fc, ctx);
-                       if (!dyn_array_fc->length_field_path) {
-                               ret = -1;
-                               goto end;
-                       }
+       if (bt_field_class_type_is(fc->type,
+                       BT_FIELD_CLASS_TYPE_OPTION_WITH_SELECTOR_FIELD)) {
+               struct bt_field_class_option_with_selector_field *opt_fc = (void *) fc;
+
+               BT_ASSERT(opt_fc->selector_fc);
+               BT_ASSERT(!opt_fc->selector_field_path);
+               opt_fc->selector_field_path = resolve_field_path(
+                       fc, opt_fc->selector_fc, ctx);
+               if (!opt_fc->selector_field_path) {
+                       ret = -1;
+                       goto end;
                }
-
-               break;
-       }
-       case BT_FIELD_CLASS_TYPE_VARIANT:
-       {
-               struct bt_field_class_variant *var_fc = (void *) fc;
+       } else if (fc->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD) {
+               struct bt_field_class_array_dynamic *dyn_array_fc = (void *) fc;
+
+               BT_ASSERT(dyn_array_fc->length_fc);
+               BT_ASSERT(!dyn_array_fc->length_field_path);
+               dyn_array_fc->length_field_path = resolve_field_path(
+                       fc, dyn_array_fc->length_fc, ctx);
+               if (!dyn_array_fc->length_field_path) {
+                       ret = -1;
+                       goto end;
+               }
+       } else if (bt_field_class_type_is(fc->type,
+                       BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD)) {
+               struct bt_field_class_variant_with_selector_field *var_fc =
+                       (void *) fc;
 
                if (var_fc->selector_fc) {
                        BT_ASSERT(!var_fc->selector_field_path);
                        var_fc->selector_field_path =
                                resolve_field_path(fc,
-                                       var_fc->selector_fc, ctx);
+                                       (void *) var_fc->selector_fc, ctx);
                        if (!var_fc->selector_field_path) {
                                ret = -1;
                                goto end;
                        }
                }
        }
-       default:
-               break;
-       }
 
        /* Recursive part */
-       switch (fc->type) {
-       case BT_FIELD_CLASS_TYPE_STRUCTURE:
-       case BT_FIELD_CLASS_TYPE_VARIANT:
-       {
+       if (bt_field_class_type_is(fc->type, BT_FIELD_CLASS_TYPE_OPTION)) {
+               struct bt_field_class_option *opt_fc = (void *) fc;
+
+               ret = bt_resolve_field_paths(opt_fc->content_fc, ctx);
+       } else if (fc->type == BT_FIELD_CLASS_TYPE_STRUCTURE ||
+                       bt_field_class_type_is(fc->type,
+                               BT_FIELD_CLASS_TYPE_VARIANT)) {
                struct bt_field_class_named_field_class_container *container_fc =
                        (void *) fc;
                uint64_t i;
 
                for (i = 0; i < container_fc->named_fcs->len; i++) {
                        struct bt_named_field_class *named_fc =
-                               BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
-                                       container_fc, i);
+                               container_fc->named_fcs->pdata[i];
 
                        ret = bt_resolve_field_paths(named_fc->fc, ctx);
                        if (ret) {
                                goto end;
                        }
                }
-
-               break;
-       }
-       case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
-       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
-       {
+       } else if (bt_field_class_type_is(fc->type,
+                       BT_FIELD_CLASS_TYPE_ARRAY)) {
                struct bt_field_class_array *array_fc = (void *) fc;
 
                ret = bt_resolve_field_paths(array_fc->element_fc, ctx);
-               break;
-       }
-       default:
-               break;
        }
 
 end:
This page took 0.02936 seconds and 4 git commands to generate.