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