| 1 | /* |
| 2 | * types.c |
| 3 | * |
| 4 | * BabelTrace - Converter |
| 5 | * |
| 6 | * Types registry. |
| 7 | * |
| 8 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 9 | * |
| 10 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 11 | * |
| 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 13 | * of this software and associated documentation files (the "Software"), to deal |
| 14 | * in the Software without restriction, including without limitation the rights |
| 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 16 | * copies of the Software, and to permit persons to whom the Software is |
| 17 | * furnished to do so, subject to the following conditions: |
| 18 | * |
| 19 | * The above copyright notice and this permission notice shall be included in |
| 20 | * all copies or substantial portions of the Software. |
| 21 | */ |
| 22 | |
| 23 | #include <babeltrace/format.h> |
| 24 | #include <babeltrace/babeltrace-internal.h> |
| 25 | #include <limits.h> |
| 26 | #include <glib.h> |
| 27 | #include <errno.h> |
| 28 | |
| 29 | static |
| 30 | GQuark prefix_quark(const char *prefix, GQuark quark) |
| 31 | { |
| 32 | GQuark nq; |
| 33 | GString *str; |
| 34 | |
| 35 | str = g_string_new(prefix); |
| 36 | g_string_append(str, g_quark_to_string(quark)); |
| 37 | nq = g_quark_from_string(g_string_free(str, FALSE)); |
| 38 | return nq; |
| 39 | } |
| 40 | |
| 41 | static |
| 42 | struct declaration * |
| 43 | lookup_declaration_scope(GQuark declaration_name, |
| 44 | struct declaration_scope *scope) |
| 45 | { |
| 46 | return g_hash_table_lookup(scope->typedef_declarations, |
| 47 | (gconstpointer) (unsigned long) declaration_name); |
| 48 | } |
| 49 | |
| 50 | struct declaration *lookup_declaration(GQuark declaration_name, |
| 51 | struct declaration_scope *scope) |
| 52 | { |
| 53 | struct declaration *declaration; |
| 54 | |
| 55 | while (scope) { |
| 56 | declaration = lookup_declaration_scope(declaration_name, |
| 57 | scope); |
| 58 | if (declaration) |
| 59 | return declaration; |
| 60 | scope = scope->parent_scope; |
| 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | int register_declaration(GQuark name, struct declaration *declaration, |
| 66 | struct declaration_scope *scope) |
| 67 | { |
| 68 | if (!name) |
| 69 | return -EPERM; |
| 70 | |
| 71 | /* Only lookup in local scope */ |
| 72 | if (lookup_declaration_scope(name, scope)) |
| 73 | return -EEXIST; |
| 74 | |
| 75 | g_hash_table_insert(scope->typedef_declarations, |
| 76 | (gpointer) (unsigned long) name, |
| 77 | declaration); |
| 78 | declaration_ref(declaration); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static |
| 83 | struct definition * |
| 84 | lookup_field_definition_scope(GQuark field_name, |
| 85 | struct definition_scope *scope) |
| 86 | { |
| 87 | return g_hash_table_lookup(scope->definitions, |
| 88 | (gconstpointer) (unsigned long) field_name); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Returns the index at which the paths differ. |
| 93 | * If the value returned equals len, it means the paths are identical |
| 94 | * from index 0 to len-1. |
| 95 | */ |
| 96 | static int compare_paths(GArray *a, GArray *b, int len) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | assert(len <= a->len); |
| 101 | assert(len <= b->len); |
| 102 | |
| 103 | for (i = 0; i < len; i++) { |
| 104 | GQuark qa, qb; |
| 105 | |
| 106 | qa = g_array_index(a, GQuark, i); |
| 107 | qb = g_array_index(b, GQuark, i); |
| 108 | if (qa != qb) |
| 109 | return i; |
| 110 | } |
| 111 | return i; |
| 112 | } |
| 113 | |
| 114 | static int is_path_child_of(GArray *path, GArray *maybe_parent) |
| 115 | { |
| 116 | int i, ret; |
| 117 | |
| 118 | if (babeltrace_debug) { |
| 119 | int need_dot = 0; |
| 120 | |
| 121 | printf_debug("Is path \""); |
| 122 | for (i = 0; i < path->len; need_dot = 1, i++) |
| 123 | printf("%s%s", need_dot ? "." : "", |
| 124 | g_quark_to_string(g_array_index(path, GQuark, i))); |
| 125 | need_dot = 0; |
| 126 | printf("\" child of \""); |
| 127 | for (i = 0; i < maybe_parent->len; need_dot = 1, i++) |
| 128 | printf("%s%s", need_dot ? "." : "", |
| 129 | g_quark_to_string(g_array_index(maybe_parent, GQuark, i))); |
| 130 | printf("\" ? "); |
| 131 | } |
| 132 | |
| 133 | if (path->len <= maybe_parent->len) { |
| 134 | ret = 0; |
| 135 | goto end; |
| 136 | } |
| 137 | if (compare_paths(path, maybe_parent, maybe_parent->len) |
| 138 | == maybe_parent->len) |
| 139 | ret = 1; |
| 140 | else |
| 141 | ret = 0; |
| 142 | end: |
| 143 | if (babeltrace_debug) |
| 144 | printf("%s\n", ret ? "Yes" : "No"); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static struct definition_scope * |
| 149 | get_definition_scope(struct definition *definition) |
| 150 | { |
| 151 | return definition->scope; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * OK, here is the fun. We want to lookup a field that is: |
| 156 | * - either in the same dynamic scope: |
| 157 | * - either in the current scope, but prior to the current field. |
| 158 | * - or in a parent scope (or parent of parent ...) still in a field |
| 159 | * prior to the current field position within the parents. |
| 160 | * - or in a different dynamic scope: |
| 161 | * - either in a upper dynamic scope (walk down a targeted scope from |
| 162 | * the dynamic scope root) |
| 163 | * - or in a lower dynamic scope (failure) |
| 164 | * The dynamic scope roots are linked together, so we can access the |
| 165 | * parent dynamic scope from the child dynamic scope by walking up to |
| 166 | * the parent. |
| 167 | * If we cannot find such a field that is prior to our current path, we |
| 168 | * return NULL. |
| 169 | * |
| 170 | * cur_path: the path leading to the variant definition. |
| 171 | * lookup_path: the path leading to the enum we want to look for. |
| 172 | * scope: the definition scope containing the variant definition. |
| 173 | */ |
| 174 | struct definition * |
| 175 | lookup_path_definition(GArray *cur_path, |
| 176 | GArray *lookup_path, |
| 177 | struct definition_scope *scope) |
| 178 | { |
| 179 | struct definition *definition, *lookup_definition; |
| 180 | GQuark last; |
| 181 | int index; |
| 182 | |
| 183 | /* Going up in the hierarchy. Check where we come from. */ |
| 184 | assert(is_path_child_of(cur_path, scope->scope_path)); |
| 185 | assert(cur_path->len - scope->scope_path->len == 1); |
| 186 | |
| 187 | /* |
| 188 | * First, check if the target name is size one, present in |
| 189 | * our parent path, located prior to us. |
| 190 | */ |
| 191 | if (lookup_path->len == 1) { |
| 192 | last = g_array_index(lookup_path, GQuark, 0); |
| 193 | lookup_definition = lookup_field_definition_scope(last, scope); |
| 194 | last = g_array_index(cur_path, GQuark, cur_path->len - 1); |
| 195 | definition = lookup_field_definition_scope(last, scope); |
| 196 | assert(definition); |
| 197 | if (lookup_definition && lookup_definition->index < definition->index) |
| 198 | return lookup_definition; |
| 199 | else |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | while (scope) { |
| 204 | if (is_path_child_of(cur_path, scope->scope_path) && |
| 205 | cur_path->len - scope->scope_path->len == 1) { |
| 206 | last = g_array_index(cur_path, GQuark, cur_path->len - 1); |
| 207 | definition = lookup_field_definition_scope(last, scope); |
| 208 | assert(definition); |
| 209 | index = definition->index; |
| 210 | } else { |
| 211 | /* |
| 212 | * Getting to a dynamic scope parent. We are |
| 213 | * guaranteed that the parent is entirely |
| 214 | * located before the child. |
| 215 | */ |
| 216 | index = -1; |
| 217 | } |
| 218 | lookup: |
| 219 | if (is_path_child_of(lookup_path, scope->scope_path)) { |
| 220 | /* Means we can lookup the field in this scope */ |
| 221 | last = g_array_index(lookup_path, GQuark, |
| 222 | scope->scope_path->len); |
| 223 | lookup_definition = lookup_field_definition_scope(last, scope); |
| 224 | if (!lookup_definition || ((index != -1) && lookup_definition->index >= index)) |
| 225 | return NULL; |
| 226 | /* Found it! And it is prior to the current field. */ |
| 227 | if (lookup_path->len - scope->scope_path->len == 1) { |
| 228 | /* Direct child */ |
| 229 | return lookup_definition; |
| 230 | } else { |
| 231 | scope = get_definition_scope(lookup_definition); |
| 232 | /* Check if the definition has a sub-scope */ |
| 233 | if (!scope) |
| 234 | return NULL; |
| 235 | /* |
| 236 | * Don't compare index anymore, because we are |
| 237 | * going within a scope that has been validated |
| 238 | * to be entirely prior to the current scope. |
| 239 | */ |
| 240 | cur_path = NULL; |
| 241 | index = -1; |
| 242 | goto lookup; |
| 243 | } |
| 244 | } else { |
| 245 | /* lookup_path is within an upper scope */ |
| 246 | cur_path = scope->scope_path; |
| 247 | scope = scope->parent_scope; |
| 248 | } |
| 249 | } |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | int register_field_definition(GQuark field_name, struct definition *definition, |
| 254 | struct definition_scope *scope) |
| 255 | { |
| 256 | if (!scope || !field_name) |
| 257 | return -EPERM; |
| 258 | |
| 259 | /* Only lookup in local scope */ |
| 260 | if (lookup_field_definition_scope(field_name, scope)) |
| 261 | return -EEXIST; |
| 262 | |
| 263 | g_hash_table_insert(scope->definitions, |
| 264 | (gpointer) (unsigned long) field_name, |
| 265 | definition); |
| 266 | /* Don't keep reference on definition */ |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | void declaration_ref(struct declaration *declaration) |
| 271 | { |
| 272 | declaration->ref++; |
| 273 | } |
| 274 | |
| 275 | void declaration_unref(struct declaration *declaration) |
| 276 | { |
| 277 | if (!declaration) |
| 278 | return; |
| 279 | if (!--declaration->ref) |
| 280 | declaration->declaration_free(declaration); |
| 281 | } |
| 282 | |
| 283 | void definition_ref(struct definition *definition) |
| 284 | { |
| 285 | definition->ref++; |
| 286 | } |
| 287 | |
| 288 | void definition_unref(struct definition *definition) |
| 289 | { |
| 290 | if (!definition) |
| 291 | return; |
| 292 | if (!--definition->ref) |
| 293 | definition->declaration->definition_free(definition); |
| 294 | } |
| 295 | |
| 296 | struct declaration_scope * |
| 297 | new_declaration_scope(struct declaration_scope *parent_scope) |
| 298 | { |
| 299 | struct declaration_scope *scope = g_new(struct declaration_scope, 1); |
| 300 | |
| 301 | scope->typedef_declarations = g_hash_table_new_full(g_direct_hash, |
| 302 | g_direct_equal, NULL, |
| 303 | (GDestroyNotify) declaration_unref); |
| 304 | scope->struct_declarations = g_hash_table_new_full(g_direct_hash, |
| 305 | g_direct_equal, NULL, |
| 306 | (GDestroyNotify) declaration_unref); |
| 307 | scope->variant_declarations = g_hash_table_new_full(g_direct_hash, |
| 308 | g_direct_equal, NULL, |
| 309 | (GDestroyNotify) declaration_unref); |
| 310 | scope->enum_declarations = g_hash_table_new_full(g_direct_hash, |
| 311 | g_direct_equal, NULL, |
| 312 | (GDestroyNotify) declaration_unref); |
| 313 | scope->parent_scope = parent_scope; |
| 314 | return scope; |
| 315 | } |
| 316 | |
| 317 | void free_declaration_scope(struct declaration_scope *scope) |
| 318 | { |
| 319 | g_hash_table_destroy(scope->enum_declarations); |
| 320 | g_hash_table_destroy(scope->variant_declarations); |
| 321 | g_hash_table_destroy(scope->struct_declarations); |
| 322 | g_hash_table_destroy(scope->typedef_declarations); |
| 323 | g_free(scope); |
| 324 | } |
| 325 | |
| 326 | static |
| 327 | struct declaration_struct *lookup_struct_declaration_scope(GQuark struct_name, |
| 328 | struct declaration_scope *scope) |
| 329 | { |
| 330 | return g_hash_table_lookup(scope->struct_declarations, |
| 331 | (gconstpointer) (unsigned long) struct_name); |
| 332 | } |
| 333 | |
| 334 | struct declaration_struct *lookup_struct_declaration(GQuark struct_name, |
| 335 | struct declaration_scope *scope) |
| 336 | { |
| 337 | struct declaration_struct *declaration; |
| 338 | |
| 339 | while (scope) { |
| 340 | declaration = lookup_struct_declaration_scope(struct_name, scope); |
| 341 | if (declaration) |
| 342 | return declaration; |
| 343 | scope = scope->parent_scope; |
| 344 | } |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | int register_struct_declaration(GQuark struct_name, |
| 349 | struct declaration_struct *struct_declaration, |
| 350 | struct declaration_scope *scope) |
| 351 | { |
| 352 | GQuark prefix_name; |
| 353 | int ret; |
| 354 | |
| 355 | if (!struct_name) |
| 356 | return -EPERM; |
| 357 | |
| 358 | /* Only lookup in local scope */ |
| 359 | if (lookup_struct_declaration_scope(struct_name, scope)) |
| 360 | return -EEXIST; |
| 361 | |
| 362 | g_hash_table_insert(scope->struct_declarations, |
| 363 | (gpointer) (unsigned long) struct_name, |
| 364 | struct_declaration); |
| 365 | declaration_ref(&struct_declaration->p); |
| 366 | |
| 367 | /* Also add in typedef/typealias scopes */ |
| 368 | prefix_name = prefix_quark("struct ", struct_name); |
| 369 | ret = register_declaration(prefix_name, &struct_declaration->p, scope); |
| 370 | assert(!ret); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static |
| 375 | struct declaration_untagged_variant * |
| 376 | lookup_variant_declaration_scope(GQuark variant_name, |
| 377 | struct declaration_scope *scope) |
| 378 | { |
| 379 | return g_hash_table_lookup(scope->variant_declarations, |
| 380 | (gconstpointer) (unsigned long) variant_name); |
| 381 | } |
| 382 | |
| 383 | struct declaration_untagged_variant * |
| 384 | lookup_variant_declaration(GQuark variant_name, |
| 385 | struct declaration_scope *scope) |
| 386 | { |
| 387 | struct declaration_untagged_variant *declaration; |
| 388 | |
| 389 | while (scope) { |
| 390 | declaration = lookup_variant_declaration_scope(variant_name, scope); |
| 391 | if (declaration) |
| 392 | return declaration; |
| 393 | scope = scope->parent_scope; |
| 394 | } |
| 395 | return NULL; |
| 396 | } |
| 397 | |
| 398 | int register_variant_declaration(GQuark variant_name, |
| 399 | struct declaration_untagged_variant *untagged_variant_declaration, |
| 400 | struct declaration_scope *scope) |
| 401 | { |
| 402 | GQuark prefix_name; |
| 403 | int ret; |
| 404 | |
| 405 | if (!variant_name) |
| 406 | return -EPERM; |
| 407 | |
| 408 | /* Only lookup in local scope */ |
| 409 | if (lookup_variant_declaration_scope(variant_name, scope)) |
| 410 | return -EEXIST; |
| 411 | |
| 412 | g_hash_table_insert(scope->variant_declarations, |
| 413 | (gpointer) (unsigned long) variant_name, |
| 414 | untagged_variant_declaration); |
| 415 | declaration_ref(&untagged_variant_declaration->p); |
| 416 | |
| 417 | /* Also add in typedef/typealias scopes */ |
| 418 | prefix_name = prefix_quark("variant ", variant_name); |
| 419 | ret = register_declaration(prefix_name, |
| 420 | &untagged_variant_declaration->p, scope); |
| 421 | assert(!ret); |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | static |
| 426 | struct declaration_enum * |
| 427 | lookup_enum_declaration_scope(GQuark enum_name, |
| 428 | struct declaration_scope *scope) |
| 429 | { |
| 430 | return g_hash_table_lookup(scope->enum_declarations, |
| 431 | (gconstpointer) (unsigned long) enum_name); |
| 432 | } |
| 433 | |
| 434 | struct declaration_enum * |
| 435 | lookup_enum_declaration(GQuark enum_name, |
| 436 | struct declaration_scope *scope) |
| 437 | { |
| 438 | struct declaration_enum *declaration; |
| 439 | |
| 440 | while (scope) { |
| 441 | declaration = lookup_enum_declaration_scope(enum_name, scope); |
| 442 | if (declaration) |
| 443 | return declaration; |
| 444 | scope = scope->parent_scope; |
| 445 | } |
| 446 | return NULL; |
| 447 | } |
| 448 | |
| 449 | int register_enum_declaration(GQuark enum_name, |
| 450 | struct declaration_enum *enum_declaration, |
| 451 | struct declaration_scope *scope) |
| 452 | { |
| 453 | GQuark prefix_name; |
| 454 | int ret; |
| 455 | |
| 456 | if (!enum_name) |
| 457 | return -EPERM; |
| 458 | |
| 459 | /* Only lookup in local scope */ |
| 460 | if (lookup_enum_declaration_scope(enum_name, scope)) |
| 461 | return -EEXIST; |
| 462 | |
| 463 | g_hash_table_insert(scope->enum_declarations, |
| 464 | (gpointer) (unsigned long) enum_name, |
| 465 | enum_declaration); |
| 466 | declaration_ref(&enum_declaration->p); |
| 467 | |
| 468 | /* Also add in typedef/typealias scopes */ |
| 469 | prefix_name = prefix_quark("enum ", enum_name); |
| 470 | ret = register_declaration(prefix_name, &enum_declaration->p, scope); |
| 471 | assert(!ret); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static struct definition_scope * |
| 476 | _new_definition_scope(struct definition_scope *parent_scope, |
| 477 | int scope_path_len) |
| 478 | { |
| 479 | struct definition_scope *scope = g_new(struct definition_scope, 1); |
| 480 | |
| 481 | scope->definitions = g_hash_table_new(g_direct_hash, |
| 482 | g_direct_equal); |
| 483 | scope->parent_scope = parent_scope; |
| 484 | scope->scope_path = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), |
| 485 | scope_path_len); |
| 486 | g_array_set_size(scope->scope_path, scope_path_len); |
| 487 | return scope; |
| 488 | } |
| 489 | |
| 490 | GQuark new_definition_path(struct definition_scope *parent_scope, |
| 491 | GQuark field_name, const char *root_name) |
| 492 | { |
| 493 | GQuark path; |
| 494 | GString *str; |
| 495 | gchar *c_str; |
| 496 | int i; |
| 497 | int need_dot = 0; |
| 498 | |
| 499 | str = g_string_new(""); |
| 500 | if (root_name) { |
| 501 | g_string_append(str, root_name); |
| 502 | need_dot = 1; |
| 503 | } else if (parent_scope) { |
| 504 | for (i = 0; i < parent_scope->scope_path->len; i++) { |
| 505 | GQuark q = g_array_index(parent_scope->scope_path, |
| 506 | GQuark, i); |
| 507 | if (!q) |
| 508 | continue; |
| 509 | if (need_dot) |
| 510 | g_string_append(str, "."); |
| 511 | g_string_append(str, g_quark_to_string(q)); |
| 512 | need_dot = 1; |
| 513 | } |
| 514 | } |
| 515 | if (field_name) { |
| 516 | if (need_dot) |
| 517 | g_string_append(str, "."); |
| 518 | g_string_append(str, g_quark_to_string(field_name)); |
| 519 | } |
| 520 | c_str = g_string_free(str, FALSE); |
| 521 | if (c_str[0] == '\0') |
| 522 | return 0; |
| 523 | path = g_quark_from_string(c_str); |
| 524 | printf_debug("new definition path: %s\n", c_str); |
| 525 | g_free(c_str); |
| 526 | return path; |
| 527 | } |
| 528 | |
| 529 | struct definition_scope * |
| 530 | new_definition_scope(struct definition_scope *parent_scope, |
| 531 | GQuark field_name, const char *root_name) |
| 532 | { |
| 533 | struct definition_scope *scope; |
| 534 | |
| 535 | if (root_name) { |
| 536 | scope = _new_definition_scope(parent_scope, 0); |
| 537 | append_scope_path(root_name, scope->scope_path); |
| 538 | } else { |
| 539 | int scope_path_len = 1; |
| 540 | |
| 541 | assert(parent_scope); |
| 542 | scope_path_len += parent_scope->scope_path->len; |
| 543 | scope = _new_definition_scope(parent_scope, scope_path_len); |
| 544 | memcpy(scope->scope_path->data, parent_scope->scope_path->data, |
| 545 | sizeof(GQuark) * (scope_path_len - 1)); |
| 546 | g_array_index(scope->scope_path, GQuark, scope_path_len - 1) = |
| 547 | field_name; |
| 548 | } |
| 549 | if (babeltrace_debug) { |
| 550 | int i, need_dot = 0; |
| 551 | |
| 552 | printf_debug("new definition scope: "); |
| 553 | for (i = 0; i < scope->scope_path->len; need_dot = 1, i++) |
| 554 | printf("%s%s", need_dot ? "." : "", |
| 555 | g_quark_to_string(g_array_index(scope->scope_path, GQuark, i))); |
| 556 | printf("\n"); |
| 557 | } |
| 558 | return scope; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * in: path (dot separated), out: q (GArray of GQuark) |
| 563 | */ |
| 564 | void append_scope_path(const char *path, GArray *q) |
| 565 | { |
| 566 | const char *ptrbegin, *ptrend = path; |
| 567 | GQuark quark; |
| 568 | |
| 569 | for (;;) { |
| 570 | char *str; |
| 571 | size_t len; |
| 572 | |
| 573 | ptrbegin = ptrend; |
| 574 | ptrend = strchr(ptrbegin, '.'); |
| 575 | if (!ptrend) |
| 576 | break; |
| 577 | len = ptrend - ptrbegin; |
| 578 | /* Don't accept two consecutive dots */ |
| 579 | assert(len != 0); |
| 580 | str = g_new(char, len + 1); /* include \0 */ |
| 581 | memcpy(str, ptrbegin, len); |
| 582 | str[len] = '\0'; |
| 583 | quark = g_quark_from_string(str); |
| 584 | g_array_append_val(q, quark); |
| 585 | g_free(str); |
| 586 | ptrend++; /* skip current dot */ |
| 587 | } |
| 588 | /* last. Check for trailing dot (and discard). */ |
| 589 | if (ptrbegin[0] != '\0') { |
| 590 | quark = g_quark_from_string(ptrbegin); |
| 591 | g_array_append_val(q, quark); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | void free_definition_scope(struct definition_scope *scope) |
| 596 | { |
| 597 | g_array_free(scope->scope_path, TRUE); |
| 598 | g_hash_table_destroy(scope->definitions); |
| 599 | g_free(scope); |
| 600 | } |
| 601 | |
| 602 | struct definition *lookup_definition(struct definition *definition, |
| 603 | const char *field_name) |
| 604 | { |
| 605 | struct definition_scope *scope = get_definition_scope(definition); |
| 606 | |
| 607 | if (!scope) |
| 608 | return NULL; |
| 609 | |
| 610 | return lookup_field_definition_scope(g_quark_from_string(field_name), |
| 611 | scope); |
| 612 | } |
| 613 | |
| 614 | struct definition_integer *lookup_integer(struct definition *definition, |
| 615 | const char *field_name, |
| 616 | int signedness) |
| 617 | { |
| 618 | struct definition *lookup; |
| 619 | struct definition_integer *lookup_integer; |
| 620 | |
| 621 | lookup = lookup_definition(definition, field_name); |
| 622 | if (!lookup) |
| 623 | return NULL; |
| 624 | if (lookup->declaration->id != CTF_TYPE_INTEGER) |
| 625 | return NULL; |
| 626 | lookup_integer = container_of(lookup, struct definition_integer, p); |
| 627 | if (lookup_integer->declaration->signedness != signedness) |
| 628 | return NULL; |
| 629 | return lookup_integer; |
| 630 | } |
| 631 | |
| 632 | struct definition_enum *lookup_enum(struct definition *definition, |
| 633 | const char *field_name, |
| 634 | int signedness) |
| 635 | { |
| 636 | struct definition *lookup; |
| 637 | struct definition_enum *lookup_enum; |
| 638 | |
| 639 | lookup = lookup_definition(definition, field_name); |
| 640 | if (!lookup) |
| 641 | return NULL; |
| 642 | if (lookup->declaration->id != CTF_TYPE_ENUM) |
| 643 | return NULL; |
| 644 | lookup_enum = container_of(lookup, struct definition_enum, p); |
| 645 | if (lookup_enum->integer->declaration->signedness != signedness) |
| 646 | return NULL; |
| 647 | return lookup_enum; |
| 648 | } |
| 649 | |
| 650 | struct definition *lookup_variant(struct definition *definition, |
| 651 | const char *field_name) |
| 652 | { |
| 653 | struct definition *lookup; |
| 654 | struct definition_variant *lookup_variant; |
| 655 | |
| 656 | lookup = lookup_definition(definition, field_name); |
| 657 | if (!lookup) |
| 658 | return NULL; |
| 659 | if (lookup->declaration->id != CTF_TYPE_VARIANT) |
| 660 | return NULL; |
| 661 | lookup_variant = container_of(lookup, struct definition_variant, p); |
| 662 | lookup = variant_get_current_field(lookup_variant); |
| 663 | assert(lookup); |
| 664 | return lookup; |
| 665 | } |