| 1 | /* |
| 2 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "LIB/RESOLVE-FIELD-PATH" |
| 24 | #include "lib/logging.h" |
| 25 | |
| 26 | #include "lib/assert-pre.h" |
| 27 | #include "common/assert.h" |
| 28 | #include <babeltrace2/trace-ir/field-path-const.h> |
| 29 | #include <limits.h> |
| 30 | #include <stdint.h> |
| 31 | #include <inttypes.h> |
| 32 | #include <glib.h> |
| 33 | |
| 34 | #include "field-class.h" |
| 35 | #include "field-path.h" |
| 36 | #include "resolve-field-path.h" |
| 37 | |
| 38 | static |
| 39 | bool find_field_class_recursive(struct bt_field_class *fc, |
| 40 | struct bt_field_class *tgt_fc, struct bt_field_path *field_path) |
| 41 | { |
| 42 | bool found = false; |
| 43 | |
| 44 | if (tgt_fc == fc) { |
| 45 | found = true; |
| 46 | goto end; |
| 47 | } |
| 48 | |
| 49 | switch (fc->type) { |
| 50 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR: |
| 51 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR: |
| 52 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR: |
| 53 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR: |
| 54 | { |
| 55 | struct bt_field_class_option *opt_fc = (void *) fc; |
| 56 | struct bt_field_path_item item = { |
| 57 | .type = BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT, |
| 58 | .index = UINT64_C(-1), |
| 59 | }; |
| 60 | |
| 61 | bt_field_path_append_item(field_path, &item); |
| 62 | found = find_field_class_recursive(opt_fc->content_fc, |
| 63 | tgt_fc, field_path); |
| 64 | if (found) { |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | bt_field_path_remove_last_item(field_path); |
| 69 | break; |
| 70 | } |
| 71 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 72 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 73 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 74 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 75 | { |
| 76 | struct bt_field_class_named_field_class_container *container_fc = |
| 77 | (void *) fc; |
| 78 | uint64_t i; |
| 79 | |
| 80 | for (i = 0; i < container_fc->named_fcs->len; i++) { |
| 81 | struct bt_named_field_class *named_fc = |
| 82 | container_fc->named_fcs->pdata[i]; |
| 83 | struct bt_field_path_item item = { |
| 84 | .type = BT_FIELD_PATH_ITEM_TYPE_INDEX, |
| 85 | .index = i, |
| 86 | }; |
| 87 | |
| 88 | bt_field_path_append_item(field_path, &item); |
| 89 | found = find_field_class_recursive(named_fc->fc, |
| 90 | tgt_fc, field_path); |
| 91 | if (found) { |
| 92 | goto end; |
| 93 | } |
| 94 | |
| 95 | bt_field_path_remove_last_item(field_path); |
| 96 | } |
| 97 | |
| 98 | break; |
| 99 | } |
| 100 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 101 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 102 | { |
| 103 | struct bt_field_class_array *array_fc = (void *) fc; |
| 104 | struct bt_field_path_item item = { |
| 105 | .type = BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT, |
| 106 | .index = UINT64_C(-1), |
| 107 | }; |
| 108 | |
| 109 | bt_field_path_append_item(field_path, &item); |
| 110 | found = find_field_class_recursive(array_fc->element_fc, |
| 111 | tgt_fc, field_path); |
| 112 | if (found) { |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | bt_field_path_remove_last_item(field_path); |
| 117 | break; |
| 118 | } |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | end: |
| 124 | return found; |
| 125 | } |
| 126 | |
| 127 | static |
| 128 | int find_field_class(struct bt_field_class *root_fc, |
| 129 | enum bt_field_path_scope root_scope, struct bt_field_class *tgt_fc, |
| 130 | struct bt_field_path **ret_field_path) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | struct bt_field_path *field_path = NULL; |
| 134 | |
| 135 | if (!root_fc) { |
| 136 | goto end; |
| 137 | } |
| 138 | |
| 139 | field_path = bt_field_path_create(); |
| 140 | if (!field_path) { |
| 141 | ret = -1; |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | field_path->root = root_scope; |
| 146 | if (!find_field_class_recursive(root_fc, tgt_fc, field_path)) { |
| 147 | /* Not found here */ |
| 148 | BT_OBJECT_PUT_REF_AND_RESET(field_path); |
| 149 | } |
| 150 | |
| 151 | end: |
| 152 | *ret_field_path = field_path; |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static |
| 157 | struct bt_field_path *find_field_class_in_ctx(struct bt_field_class *fc, |
| 158 | struct bt_resolve_field_path_context *ctx) |
| 159 | { |
| 160 | struct bt_field_path *field_path = NULL; |
| 161 | int ret; |
| 162 | |
| 163 | ret = find_field_class(ctx->packet_context, BT_FIELD_PATH_SCOPE_PACKET_CONTEXT, |
| 164 | fc, &field_path); |
| 165 | if (ret || field_path) { |
| 166 | goto end; |
| 167 | } |
| 168 | |
| 169 | ret = find_field_class(ctx->event_common_context, |
| 170 | BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT, fc, &field_path); |
| 171 | if (ret || field_path) { |
| 172 | goto end; |
| 173 | } |
| 174 | |
| 175 | ret = find_field_class(ctx->event_specific_context, |
| 176 | BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT, fc, &field_path); |
| 177 | if (ret || field_path) { |
| 178 | goto end; |
| 179 | } |
| 180 | |
| 181 | ret = find_field_class(ctx->event_payload, BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD, |
| 182 | fc, &field_path); |
| 183 | if (ret || field_path) { |
| 184 | goto end; |
| 185 | } |
| 186 | |
| 187 | end: |
| 188 | return field_path; |
| 189 | } |
| 190 | |
| 191 | BT_ASSERT_PRE_DEV_FUNC |
| 192 | static inline |
| 193 | bool target_is_before_source(struct bt_field_path *src_field_path, |
| 194 | struct bt_field_path *tgt_field_path) |
| 195 | { |
| 196 | bool is_valid = true; |
| 197 | uint64_t src_i = 0, tgt_i = 0; |
| 198 | |
| 199 | if (tgt_field_path->root < src_field_path->root) { |
| 200 | goto end; |
| 201 | } |
| 202 | |
| 203 | if (tgt_field_path->root > src_field_path->root) { |
| 204 | is_valid = false; |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | BT_ASSERT(tgt_field_path->root == src_field_path->root); |
| 209 | |
| 210 | for (src_i = 0, tgt_i = 0; src_i < src_field_path->items->len && |
| 211 | tgt_i < tgt_field_path->items->len; src_i++, tgt_i++) { |
| 212 | struct bt_field_path_item *src_fp_item = |
| 213 | bt_field_path_borrow_item_by_index_inline( |
| 214 | src_field_path, src_i); |
| 215 | struct bt_field_path_item *tgt_fp_item = |
| 216 | bt_field_path_borrow_item_by_index_inline( |
| 217 | tgt_field_path, tgt_i); |
| 218 | |
| 219 | if (src_fp_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX && |
| 220 | tgt_fp_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX) { |
| 221 | if (tgt_fp_item->index > src_fp_item->index) { |
| 222 | is_valid = false; |
| 223 | goto end; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | src_i++; |
| 228 | tgt_i++; |
| 229 | } |
| 230 | |
| 231 | end: |
| 232 | return is_valid; |
| 233 | } |
| 234 | |
| 235 | BT_ASSERT_PRE_DEV_FUNC |
| 236 | static inline |
| 237 | struct bt_field_class *borrow_root_field_class( |
| 238 | struct bt_resolve_field_path_context *ctx, enum bt_field_path_scope scope) |
| 239 | { |
| 240 | switch (scope) { |
| 241 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
| 242 | return ctx->packet_context; |
| 243 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
| 244 | return ctx->event_common_context; |
| 245 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 246 | return ctx->event_specific_context; |
| 247 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
| 248 | return ctx->event_payload; |
| 249 | default: |
| 250 | abort(); |
| 251 | } |
| 252 | |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | BT_ASSERT_PRE_DEV_FUNC |
| 257 | static inline |
| 258 | struct bt_field_class *borrow_child_field_class( |
| 259 | struct bt_field_class *parent_fc, |
| 260 | struct bt_field_path_item *fp_item) |
| 261 | { |
| 262 | struct bt_field_class *child_fc = NULL; |
| 263 | |
| 264 | switch (parent_fc->type) { |
| 265 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR: |
| 266 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR: |
| 267 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR: |
| 268 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR: |
| 269 | { |
| 270 | struct bt_field_class_option *opt_fc = (void *) parent_fc; |
| 271 | |
| 272 | BT_ASSERT(fp_item->type == |
| 273 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_OPTION_CONTENT); |
| 274 | child_fc = opt_fc->content_fc; |
| 275 | break; |
| 276 | } |
| 277 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 278 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 279 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 280 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 281 | { |
| 282 | struct bt_field_class_named_field_class_container *container_fc = |
| 283 | (void *) parent_fc; |
| 284 | struct bt_named_field_class *named_fc; |
| 285 | |
| 286 | BT_ASSERT(fp_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 287 | named_fc = container_fc->named_fcs->pdata[fp_item->index]; |
| 288 | child_fc = named_fc->fc; |
| 289 | break; |
| 290 | } |
| 291 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 292 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 293 | { |
| 294 | struct bt_field_class_array *array_fc = (void *) parent_fc; |
| 295 | |
| 296 | BT_ASSERT(fp_item->type == |
| 297 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); |
| 298 | child_fc = array_fc->element_fc; |
| 299 | break; |
| 300 | } |
| 301 | default: |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | return child_fc; |
| 306 | } |
| 307 | |
| 308 | BT_ASSERT_PRE_DEV_FUNC |
| 309 | static inline |
| 310 | bool target_field_path_in_different_scope_has_struct_fc_only( |
| 311 | struct bt_field_path *src_field_path, |
| 312 | struct bt_field_path *tgt_field_path, |
| 313 | struct bt_resolve_field_path_context *ctx) |
| 314 | { |
| 315 | bool is_valid = true; |
| 316 | uint64_t i = 0; |
| 317 | struct bt_field_class *fc; |
| 318 | |
| 319 | if (src_field_path->root == tgt_field_path->root) { |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | fc = borrow_root_field_class(ctx, tgt_field_path->root); |
| 324 | |
| 325 | for (i = 0; i < tgt_field_path->items->len; i++) { |
| 326 | struct bt_field_path_item *fp_item = |
| 327 | bt_field_path_borrow_item_by_index_inline( |
| 328 | tgt_field_path, i); |
| 329 | |
| 330 | if (fc->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || |
| 331 | fc->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY || |
| 332 | fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR || |
| 333 | fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR || |
| 334 | fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR || |
| 335 | fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR || |
| 336 | fc->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR || |
| 337 | fc->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || |
| 338 | fc->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) { |
| 339 | is_valid = false; |
| 340 | goto end; |
| 341 | } |
| 342 | |
| 343 | BT_ASSERT(fp_item->type == BT_FIELD_PATH_ITEM_TYPE_INDEX); |
| 344 | fc = borrow_child_field_class(fc, fp_item); |
| 345 | } |
| 346 | |
| 347 | end: |
| 348 | return is_valid; |
| 349 | } |
| 350 | |
| 351 | BT_ASSERT_PRE_DEV_FUNC |
| 352 | static inline |
| 353 | bool lca_is_structure_field_class(struct bt_field_path *src_field_path, |
| 354 | struct bt_field_path *tgt_field_path, |
| 355 | struct bt_resolve_field_path_context *ctx) |
| 356 | { |
| 357 | bool is_valid = true; |
| 358 | struct bt_field_class *src_fc; |
| 359 | struct bt_field_class *tgt_fc; |
| 360 | struct bt_field_class *prev_fc = NULL; |
| 361 | uint64_t src_i = 0, tgt_i = 0; |
| 362 | |
| 363 | if (src_field_path->root != tgt_field_path->root) { |
| 364 | goto end; |
| 365 | } |
| 366 | |
| 367 | src_fc = borrow_root_field_class(ctx, src_field_path->root); |
| 368 | tgt_fc = borrow_root_field_class(ctx, tgt_field_path->root); |
| 369 | BT_ASSERT(src_fc); |
| 370 | BT_ASSERT(tgt_fc); |
| 371 | |
| 372 | for (src_i = 0, tgt_i = 0; src_i < src_field_path->items->len && |
| 373 | tgt_i < tgt_field_path->items->len; src_i++, tgt_i++) { |
| 374 | struct bt_field_path_item *src_fp_item = |
| 375 | bt_field_path_borrow_item_by_index_inline( |
| 376 | src_field_path, src_i); |
| 377 | struct bt_field_path_item *tgt_fp_item = |
| 378 | bt_field_path_borrow_item_by_index_inline( |
| 379 | tgt_field_path, tgt_i); |
| 380 | |
| 381 | if (src_fc != tgt_fc) { |
| 382 | if (!prev_fc) { |
| 383 | /* |
| 384 | * This is correct: the LCA is the root |
| 385 | * scope field class, which must be a |
| 386 | * structure field class. |
| 387 | */ |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | if (prev_fc->type != BT_FIELD_CLASS_TYPE_STRUCTURE) { |
| 392 | is_valid = false; |
| 393 | } |
| 394 | |
| 395 | break; |
| 396 | } |
| 397 | |
| 398 | prev_fc = src_fc; |
| 399 | src_fc = borrow_child_field_class(src_fc, src_fp_item); |
| 400 | tgt_fc = borrow_child_field_class(tgt_fc, tgt_fp_item); |
| 401 | } |
| 402 | |
| 403 | end: |
| 404 | return is_valid; |
| 405 | } |
| 406 | |
| 407 | BT_ASSERT_PRE_DEV_FUNC |
| 408 | static inline |
| 409 | bool lca_to_target_has_struct_fc_only(struct bt_field_path *src_field_path, |
| 410 | struct bt_field_path *tgt_field_path, |
| 411 | struct bt_resolve_field_path_context *ctx) |
| 412 | { |
| 413 | bool is_valid = true; |
| 414 | struct bt_field_class *src_fc; |
| 415 | struct bt_field_class *tgt_fc; |
| 416 | uint64_t src_i = 0, tgt_i = 0; |
| 417 | |
| 418 | if (src_field_path->root != tgt_field_path->root) { |
| 419 | goto end; |
| 420 | } |
| 421 | |
| 422 | src_fc = borrow_root_field_class(ctx, src_field_path->root); |
| 423 | tgt_fc = borrow_root_field_class(ctx, tgt_field_path->root); |
| 424 | BT_ASSERT(src_fc); |
| 425 | BT_ASSERT(tgt_fc); |
| 426 | BT_ASSERT(src_fc == tgt_fc); |
| 427 | |
| 428 | /* Find LCA */ |
| 429 | for (src_i = 0, tgt_i = 0; src_i < src_field_path->items->len && |
| 430 | tgt_i < tgt_field_path->items->len; src_i++, tgt_i++) { |
| 431 | struct bt_field_path_item *src_fp_item = |
| 432 | bt_field_path_borrow_item_by_index_inline( |
| 433 | src_field_path, src_i); |
| 434 | struct bt_field_path_item *tgt_fp_item = |
| 435 | bt_field_path_borrow_item_by_index_inline( |
| 436 | tgt_field_path, tgt_i); |
| 437 | |
| 438 | if (src_i != tgt_i) { |
| 439 | /* Next field class is different: LCA is `tgt_fc` */ |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | src_fc = borrow_child_field_class(src_fc, src_fp_item); |
| 444 | tgt_fc = borrow_child_field_class(tgt_fc, tgt_fp_item); |
| 445 | } |
| 446 | |
| 447 | /* Only structure field classes to the target */ |
| 448 | for (; tgt_i < tgt_field_path->items->len; tgt_i++) { |
| 449 | struct bt_field_path_item *tgt_fp_item = |
| 450 | bt_field_path_borrow_item_by_index_inline( |
| 451 | tgt_field_path, tgt_i); |
| 452 | |
| 453 | if (tgt_fc->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || |
| 454 | tgt_fc->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY || |
| 455 | tgt_fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR || |
| 456 | tgt_fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR || |
| 457 | tgt_fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR || |
| 458 | tgt_fc->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR || |
| 459 | tgt_fc->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR || |
| 460 | tgt_fc->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || |
| 461 | tgt_fc->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) { |
| 462 | is_valid = false; |
| 463 | goto end; |
| 464 | } |
| 465 | |
| 466 | tgt_fc = borrow_child_field_class(tgt_fc, tgt_fp_item); |
| 467 | } |
| 468 | |
| 469 | end: |
| 470 | return is_valid; |
| 471 | } |
| 472 | |
| 473 | BT_ASSERT_PRE_DEV_FUNC |
| 474 | static inline |
| 475 | bool field_path_is_valid(struct bt_field_class *src_fc, |
| 476 | struct bt_field_class *tgt_fc, |
| 477 | struct bt_resolve_field_path_context *ctx) |
| 478 | { |
| 479 | bool is_valid = true; |
| 480 | struct bt_field_path *src_field_path = find_field_class_in_ctx( |
| 481 | src_fc, ctx); |
| 482 | struct bt_field_path *tgt_field_path = find_field_class_in_ctx( |
| 483 | tgt_fc, ctx); |
| 484 | |
| 485 | if (!src_field_path) { |
| 486 | BT_ASSERT_PRE_DEV_MSG("Cannot find requesting field class in " |
| 487 | "resolving context: %!+F", src_fc); |
| 488 | is_valid = false; |
| 489 | goto end; |
| 490 | } |
| 491 | |
| 492 | if (!tgt_field_path) { |
| 493 | BT_ASSERT_PRE_DEV_MSG("Cannot find target field class in " |
| 494 | "resolving context: %!+F", tgt_fc); |
| 495 | is_valid = false; |
| 496 | goto end; |
| 497 | } |
| 498 | |
| 499 | /* Target must be before source */ |
| 500 | if (!target_is_before_source(src_field_path, tgt_field_path)) { |
| 501 | BT_ASSERT_PRE_DEV_MSG("Target field class is located after " |
| 502 | "requesting field class: %![req-fc-]+F, %![tgt-fc-]+F", |
| 503 | src_fc, tgt_fc); |
| 504 | is_valid = false; |
| 505 | goto end; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * If target is in a different scope than source, there are no |
| 510 | * array or variant field classes on the way to the target. |
| 511 | */ |
| 512 | if (!target_field_path_in_different_scope_has_struct_fc_only( |
| 513 | src_field_path, tgt_field_path, ctx)) { |
| 514 | BT_ASSERT_PRE_DEV_MSG("Target field class is located in a " |
| 515 | "different scope than requesting field class, " |
| 516 | "but within an array or a variant field class: " |
| 517 | "%![req-fc-]+F, %![tgt-fc-]+F", |
| 518 | src_fc, tgt_fc); |
| 519 | is_valid = false; |
| 520 | goto end; |
| 521 | } |
| 522 | |
| 523 | /* Same scope: LCA must be a structure field class */ |
| 524 | if (!lca_is_structure_field_class(src_field_path, tgt_field_path, ctx)) { |
| 525 | BT_ASSERT_PRE_DEV_MSG("Lowest common ancestor of target and " |
| 526 | "requesting field classes is not a structure field class: " |
| 527 | "%![req-fc-]+F, %![tgt-fc-]+F", |
| 528 | src_fc, tgt_fc); |
| 529 | is_valid = false; |
| 530 | goto end; |
| 531 | } |
| 532 | |
| 533 | /* Same scope: path from LCA to target has no array/variant FTs */ |
| 534 | if (!lca_to_target_has_struct_fc_only(src_field_path, tgt_field_path, |
| 535 | ctx)) { |
| 536 | BT_ASSERT_PRE_DEV_MSG("Path from lowest common ancestor of target " |
| 537 | "and requesting field classes to target field class " |
| 538 | "contains an array or a variant field class: " |
| 539 | "%![req-fc-]+F, %![tgt-fc-]+F", src_fc, tgt_fc); |
| 540 | is_valid = false; |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | end: |
| 545 | bt_object_put_ref(src_field_path); |
| 546 | bt_object_put_ref(tgt_field_path); |
| 547 | return is_valid; |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | struct bt_field_path *resolve_field_path(struct bt_field_class *src_fc, |
| 552 | struct bt_field_class *tgt_fc, |
| 553 | struct bt_resolve_field_path_context *ctx) |
| 554 | { |
| 555 | BT_ASSERT_PRE_DEV(field_path_is_valid(src_fc, tgt_fc, ctx), |
| 556 | "Invalid target field class: %![req-fc-]+F, %![tgt-fc-]+F", |
| 557 | src_fc, tgt_fc); |
| 558 | return find_field_class_in_ctx(tgt_fc, ctx); |
| 559 | } |
| 560 | |
| 561 | BT_HIDDEN |
| 562 | int bt_resolve_field_paths(struct bt_field_class *fc, |
| 563 | struct bt_resolve_field_path_context *ctx) |
| 564 | { |
| 565 | int ret = 0; |
| 566 | |
| 567 | BT_ASSERT(fc); |
| 568 | |
| 569 | /* Resolving part for dynamic array and variant field classes */ |
| 570 | switch (fc->type) { |
| 571 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR: |
| 572 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR: |
| 573 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR: |
| 574 | { |
| 575 | struct bt_field_class_option_with_selector *opt_fc = (void *) fc; |
| 576 | |
| 577 | if (opt_fc->selector_fc) { |
| 578 | BT_ASSERT(!opt_fc->selector_field_path); |
| 579 | opt_fc->selector_field_path = resolve_field_path( |
| 580 | fc, opt_fc->selector_fc, ctx); |
| 581 | if (!opt_fc->selector_field_path) { |
| 582 | ret = -1; |
| 583 | goto end; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | break; |
| 588 | } |
| 589 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 590 | { |
| 591 | struct bt_field_class_array_dynamic *dyn_array_fc = (void *) fc; |
| 592 | |
| 593 | if (dyn_array_fc->length_fc) { |
| 594 | BT_ASSERT(!dyn_array_fc->length_field_path); |
| 595 | dyn_array_fc->length_field_path = resolve_field_path( |
| 596 | fc, dyn_array_fc->length_fc, ctx); |
| 597 | if (!dyn_array_fc->length_field_path) { |
| 598 | ret = -1; |
| 599 | goto end; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | break; |
| 604 | } |
| 605 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 606 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 607 | { |
| 608 | struct bt_field_class_variant_with_selector *var_fc = |
| 609 | (void *) fc; |
| 610 | |
| 611 | if (var_fc->selector_fc) { |
| 612 | BT_ASSERT(!var_fc->selector_field_path); |
| 613 | var_fc->selector_field_path = |
| 614 | resolve_field_path(fc, |
| 615 | (void *) var_fc->selector_fc, ctx); |
| 616 | if (!var_fc->selector_field_path) { |
| 617 | ret = -1; |
| 618 | goto end; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | default: |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | /* Recursive part */ |
| 627 | switch (fc->type) { |
| 628 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR: |
| 629 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR: |
| 630 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR: |
| 631 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR: |
| 632 | { |
| 633 | struct bt_field_class_option *opt_fc = (void *) fc; |
| 634 | |
| 635 | ret = bt_resolve_field_paths(opt_fc->content_fc, ctx); |
| 636 | break; |
| 637 | } |
| 638 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 639 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
| 640 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: |
| 641 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: |
| 642 | { |
| 643 | struct bt_field_class_named_field_class_container *container_fc = |
| 644 | (void *) fc; |
| 645 | uint64_t i; |
| 646 | |
| 647 | for (i = 0; i < container_fc->named_fcs->len; i++) { |
| 648 | struct bt_named_field_class *named_fc = |
| 649 | container_fc->named_fcs->pdata[i]; |
| 650 | |
| 651 | ret = bt_resolve_field_paths(named_fc->fc, ctx); |
| 652 | if (ret) { |
| 653 | goto end; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | break; |
| 658 | } |
| 659 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 660 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
| 661 | { |
| 662 | struct bt_field_class_array *array_fc = (void *) fc; |
| 663 | |
| 664 | ret = bt_resolve_field_paths(array_fc->element_fc, ctx); |
| 665 | break; |
| 666 | } |
| 667 | default: |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | end: |
| 672 | return ret; |
| 673 | } |