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