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> |
6a36ddca | 25 | #include <limits.h> |
6dc2ca62 | 26 | #include <glib.h> |
d1708134 MD |
27 | #include <errno.h> |
28 | ||
c054553d | 29 | static |
78af2bcd MD |
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, | |
f6625916 | 44 | struct declaration_scope *scope) |
d1708134 | 45 | { |
f6625916 MD |
46 | return g_hash_table_lookup(scope->typedef_declarations, |
47 | (gconstpointer) (unsigned long) declaration_name); | |
d1708134 MD |
48 | } |
49 | ||
78af2bcd | 50 | struct declaration *lookup_declaration(GQuark declaration_name, |
f6625916 | 51 | struct declaration_scope *scope) |
c054553d | 52 | { |
78af2bcd | 53 | struct declaration *declaration; |
c054553d MD |
54 | |
55 | while (scope) { | |
78af2bcd MD |
56 | declaration = lookup_declaration_scope(declaration_name, |
57 | scope); | |
58 | if (declaration) | |
59 | return declaration; | |
c054553d MD |
60 | scope = scope->parent_scope; |
61 | } | |
62 | return NULL; | |
63 | } | |
64 | ||
78af2bcd | 65 | int register_declaration(GQuark name, struct declaration *declaration, |
f6625916 | 66 | struct declaration_scope *scope) |
d1708134 | 67 | { |
64893f33 | 68 | if (!name) |
6ee5115e MD |
69 | return -EPERM; |
70 | ||
c054553d | 71 | /* Only lookup in local scope */ |
78af2bcd | 72 | if (lookup_declaration_scope(name, scope)) |
d1708134 MD |
73 | return -EEXIST; |
74 | ||
f6625916 | 75 | g_hash_table_insert(scope->typedef_declarations, |
64893f33 | 76 | (gpointer) (unsigned long) name, |
f6625916 MD |
77 | declaration); |
78 | declaration_ref(declaration); | |
ac88af75 MD |
79 | return 0; |
80 | } | |
81 | ||
82 | static | |
e1151715 | 83 | struct definition * |
f6625916 MD |
84 | lookup_field_definition_scope(GQuark field_name, |
85 | struct definition_scope *scope) | |
ac88af75 | 86 | { |
e1151715 | 87 | return g_hash_table_lookup(scope->definitions, |
ac88af75 MD |
88 | (gconstpointer) (unsigned long) field_name); |
89 | } | |
90 | ||
05c749e5 MD |
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 | { | |
98df1c9f MD |
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 | } | |
05c749e5 MD |
137 | if (compare_paths(path, maybe_parent, maybe_parent->len) |
138 | == maybe_parent->len) | |
98df1c9f | 139 | ret = 1; |
05c749e5 | 140 | else |
98df1c9f MD |
141 | ret = 0; |
142 | end: | |
143 | if (babeltrace_debug) | |
144 | printf("%s\n", ret ? "Yes" : "No"); | |
145 | return ret; | |
05c749e5 MD |
146 | } |
147 | ||
148 | static struct definition_scope * | |
149 | get_definition_scope(struct definition *definition) | |
150 | { | |
a35173fe | 151 | return definition->scope; |
05c749e5 MD |
152 | } |
153 | ||
154 | /* | |
155 | * OK, here is the fun. We want to lookup a field that is: | |
9e29e16e MD |
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. | |
05c749e5 MD |
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 | */ | |
e1151715 | 174 | struct definition * |
a35173fe MD |
175 | lookup_path_definition(GArray *cur_path, |
176 | GArray *lookup_path, | |
177 | struct definition_scope *scope) | |
ac88af75 | 178 | { |
05c749e5 MD |
179 | struct definition *definition, *lookup_definition; |
180 | GQuark last; | |
181 | int index; | |
ac88af75 | 182 | |
98df1c9f MD |
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); | |
05c749e5 MD |
194 | last = g_array_index(cur_path, GQuark, cur_path->len - 1); |
195 | definition = lookup_field_definition_scope(last, scope); | |
196 | assert(definition); | |
98df1c9f MD |
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 | } | |
05c749e5 MD |
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 { | |
05c749e5 MD |
245 | /* lookup_path is within an upper scope */ |
246 | cur_path = scope->scope_path; | |
247 | scope = scope->parent_scope; | |
248 | } | |
ac88af75 MD |
249 | } |
250 | return NULL; | |
251 | } | |
252 | ||
e1151715 | 253 | int register_field_definition(GQuark field_name, struct definition *definition, |
f6625916 | 254 | struct definition_scope *scope) |
ac88af75 | 255 | { |
98df1c9f | 256 | if (!scope || !field_name) |
ac88af75 MD |
257 | return -EPERM; |
258 | ||
259 | /* Only lookup in local scope */ | |
e1151715 | 260 | if (lookup_field_definition_scope(field_name, scope)) |
ac88af75 MD |
261 | return -EEXIST; |
262 | ||
e1151715 | 263 | g_hash_table_insert(scope->definitions, |
ac88af75 | 264 | (gpointer) (unsigned long) field_name, |
e1151715 | 265 | definition); |
98df1c9f | 266 | /* Don't keep reference on definition */ |
d1708134 MD |
267 | return 0; |
268 | } | |
269 | ||
f6625916 | 270 | void declaration_ref(struct declaration *declaration) |
4c8bfb7e | 271 | { |
f6625916 | 272 | declaration->ref++; |
4c8bfb7e MD |
273 | } |
274 | ||
f6625916 | 275 | void declaration_unref(struct declaration *declaration) |
4c8bfb7e | 276 | { |
ff00cad2 MD |
277 | if (!declaration) |
278 | return; | |
f6625916 MD |
279 | if (!--declaration->ref) |
280 | declaration->declaration_free(declaration); | |
4c8bfb7e MD |
281 | } |
282 | ||
e1151715 | 283 | void definition_ref(struct definition *definition) |
d1708134 | 284 | { |
e1151715 | 285 | definition->ref++; |
d1708134 MD |
286 | } |
287 | ||
e1151715 | 288 | void definition_unref(struct definition *definition) |
d1708134 | 289 | { |
ff00cad2 MD |
290 | if (!definition) |
291 | return; | |
e1151715 | 292 | if (!--definition->ref) |
f6625916 | 293 | definition->declaration->definition_free(definition); |
c054553d MD |
294 | } |
295 | ||
f6625916 MD |
296 | struct declaration_scope * |
297 | new_declaration_scope(struct declaration_scope *parent_scope) | |
c054553d | 298 | { |
f6625916 | 299 | struct declaration_scope *scope = g_new(struct declaration_scope, 1); |
c054553d | 300 | |
f6625916 | 301 | scope->typedef_declarations = g_hash_table_new_full(g_direct_hash, |
c13cbf74 | 302 | g_direct_equal, NULL, |
7d11cac6 | 303 | (GDestroyNotify) declaration_unref); |
f6625916 | 304 | scope->struct_declarations = g_hash_table_new_full(g_direct_hash, |
c13cbf74 | 305 | g_direct_equal, NULL, |
f6625916 MD |
306 | (GDestroyNotify) declaration_unref); |
307 | scope->variant_declarations = g_hash_table_new_full(g_direct_hash, | |
c13cbf74 | 308 | g_direct_equal, NULL, |
f6625916 MD |
309 | (GDestroyNotify) declaration_unref); |
310 | scope->enum_declarations = g_hash_table_new_full(g_direct_hash, | |
c054553d | 311 | g_direct_equal, NULL, |
f6625916 | 312 | (GDestroyNotify) declaration_unref); |
64893f33 MD |
313 | scope->parent_scope = parent_scope; |
314 | return scope; | |
315 | } | |
316 | ||
f6625916 | 317 | void free_declaration_scope(struct declaration_scope *scope) |
64893f33 | 318 | { |
f6625916 MD |
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); | |
64893f33 MD |
323 | g_free(scope); |
324 | } | |
325 | ||
c13cbf74 | 326 | static |
f6625916 MD |
327 | struct declaration_struct *lookup_struct_declaration_scope(GQuark struct_name, |
328 | struct declaration_scope *scope) | |
c13cbf74 | 329 | { |
f6625916 | 330 | return g_hash_table_lookup(scope->struct_declarations, |
c13cbf74 MD |
331 | (gconstpointer) (unsigned long) struct_name); |
332 | } | |
333 | ||
f6625916 MD |
334 | struct declaration_struct *lookup_struct_declaration(GQuark struct_name, |
335 | struct declaration_scope *scope) | |
c13cbf74 | 336 | { |
f6625916 | 337 | struct declaration_struct *declaration; |
c13cbf74 MD |
338 | |
339 | while (scope) { | |
f6625916 MD |
340 | declaration = lookup_struct_declaration_scope(struct_name, scope); |
341 | if (declaration) | |
342 | return declaration; | |
c13cbf74 MD |
343 | scope = scope->parent_scope; |
344 | } | |
345 | return NULL; | |
346 | } | |
347 | ||
f6625916 MD |
348 | int register_struct_declaration(GQuark struct_name, |
349 | struct declaration_struct *struct_declaration, | |
350 | struct declaration_scope *scope) | |
c13cbf74 | 351 | { |
78af2bcd MD |
352 | GQuark prefix_name; |
353 | int ret; | |
354 | ||
c13cbf74 MD |
355 | if (!struct_name) |
356 | return -EPERM; | |
357 | ||
358 | /* Only lookup in local scope */ | |
f6625916 | 359 | if (lookup_struct_declaration_scope(struct_name, scope)) |
c13cbf74 MD |
360 | return -EEXIST; |
361 | ||
f6625916 | 362 | g_hash_table_insert(scope->struct_declarations, |
c13cbf74 | 363 | (gpointer) (unsigned long) struct_name, |
f6625916 MD |
364 | struct_declaration); |
365 | declaration_ref(&struct_declaration->p); | |
78af2bcd MD |
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); | |
c13cbf74 MD |
371 | return 0; |
372 | } | |
373 | ||
374 | static | |
a0720417 | 375 | struct declaration_untagged_variant * |
f6625916 MD |
376 | lookup_variant_declaration_scope(GQuark variant_name, |
377 | struct declaration_scope *scope) | |
c13cbf74 | 378 | { |
f6625916 | 379 | return g_hash_table_lookup(scope->variant_declarations, |
c13cbf74 MD |
380 | (gconstpointer) (unsigned long) variant_name); |
381 | } | |
382 | ||
a0720417 | 383 | struct declaration_untagged_variant * |
f6625916 MD |
384 | lookup_variant_declaration(GQuark variant_name, |
385 | struct declaration_scope *scope) | |
c13cbf74 | 386 | { |
a0720417 | 387 | struct declaration_untagged_variant *declaration; |
c13cbf74 MD |
388 | |
389 | while (scope) { | |
f6625916 MD |
390 | declaration = lookup_variant_declaration_scope(variant_name, scope); |
391 | if (declaration) | |
392 | return declaration; | |
c13cbf74 MD |
393 | scope = scope->parent_scope; |
394 | } | |
395 | return NULL; | |
396 | } | |
397 | ||
f6625916 | 398 | int register_variant_declaration(GQuark variant_name, |
a0720417 | 399 | struct declaration_untagged_variant *untagged_variant_declaration, |
f6625916 | 400 | struct declaration_scope *scope) |
c13cbf74 | 401 | { |
78af2bcd MD |
402 | GQuark prefix_name; |
403 | int ret; | |
404 | ||
c13cbf74 MD |
405 | if (!variant_name) |
406 | return -EPERM; | |
407 | ||
408 | /* Only lookup in local scope */ | |
f6625916 | 409 | if (lookup_variant_declaration_scope(variant_name, scope)) |
c13cbf74 MD |
410 | return -EEXIST; |
411 | ||
f6625916 | 412 | g_hash_table_insert(scope->variant_declarations, |
c13cbf74 | 413 | (gpointer) (unsigned long) variant_name, |
a0720417 MD |
414 | untagged_variant_declaration); |
415 | declaration_ref(&untagged_variant_declaration->p); | |
78af2bcd MD |
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); | |
c13cbf74 MD |
422 | return 0; |
423 | } | |
424 | ||
425 | static | |
f6625916 MD |
426 | struct declaration_enum * |
427 | lookup_enum_declaration_scope(GQuark enum_name, | |
428 | struct declaration_scope *scope) | |
c13cbf74 | 429 | { |
f6625916 | 430 | return g_hash_table_lookup(scope->enum_declarations, |
c13cbf74 MD |
431 | (gconstpointer) (unsigned long) enum_name); |
432 | } | |
433 | ||
f6625916 MD |
434 | struct declaration_enum * |
435 | lookup_enum_declaration(GQuark enum_name, | |
436 | struct declaration_scope *scope) | |
c13cbf74 | 437 | { |
f6625916 | 438 | struct declaration_enum *declaration; |
c13cbf74 MD |
439 | |
440 | while (scope) { | |
f6625916 MD |
441 | declaration = lookup_enum_declaration_scope(enum_name, scope); |
442 | if (declaration) | |
443 | return declaration; | |
c13cbf74 MD |
444 | scope = scope->parent_scope; |
445 | } | |
446 | return NULL; | |
447 | } | |
448 | ||
f6625916 MD |
449 | int register_enum_declaration(GQuark enum_name, |
450 | struct declaration_enum *enum_declaration, | |
451 | struct declaration_scope *scope) | |
c13cbf74 | 452 | { |
78af2bcd MD |
453 | GQuark prefix_name; |
454 | int ret; | |
455 | ||
c13cbf74 MD |
456 | if (!enum_name) |
457 | return -EPERM; | |
458 | ||
459 | /* Only lookup in local scope */ | |
f6625916 | 460 | if (lookup_enum_declaration_scope(enum_name, scope)) |
c13cbf74 MD |
461 | return -EEXIST; |
462 | ||
f6625916 | 463 | g_hash_table_insert(scope->enum_declarations, |
c13cbf74 | 464 | (gpointer) (unsigned long) enum_name, |
f6625916 MD |
465 | enum_declaration); |
466 | declaration_ref(&enum_declaration->p); | |
78af2bcd MD |
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); | |
c13cbf74 MD |
472 | return 0; |
473 | } | |
474 | ||
9e29e16e MD |
475 | static struct definition_scope * |
476 | _new_definition_scope(struct definition_scope *parent_scope, | |
477 | int scope_path_len) | |
64893f33 | 478 | { |
e1151715 | 479 | struct definition_scope *scope = g_new(struct definition_scope, 1); |
64893f33 | 480 | |
98df1c9f MD |
481 | scope->definitions = g_hash_table_new(g_direct_hash, |
482 | g_direct_equal); | |
c054553d | 483 | scope->parent_scope = parent_scope; |
05c749e5 MD |
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); | |
9e29e16e MD |
487 | return scope; |
488 | } | |
489 | ||
98df1c9f MD |
490 | GQuark new_definition_path(struct definition_scope *parent_scope, |
491 | GQuark field_name, const char *root_name) | |
31262354 MD |
492 | { |
493 | GQuark path; | |
494 | GString *str; | |
495 | gchar *c_str; | |
496 | int i; | |
98df1c9f | 497 | int need_dot = 0; |
31262354 MD |
498 | |
499 | str = g_string_new(""); | |
98df1c9f MD |
500 | if (root_name) { |
501 | g_string_append(str, root_name); | |
502 | need_dot = 1; | |
503 | } else if (parent_scope) { | |
31262354 MD |
504 | for (i = 0; i < parent_scope->scope_path->len; i++) { |
505 | GQuark q = g_array_index(parent_scope->scope_path, | |
506 | GQuark, i); | |
b7e35bad MD |
507 | if (!q) |
508 | continue; | |
98df1c9f MD |
509 | if (need_dot) |
510 | g_string_append(str, "."); | |
31262354 | 511 | g_string_append(str, g_quark_to_string(q)); |
98df1c9f | 512 | need_dot = 1; |
31262354 MD |
513 | } |
514 | } | |
98df1c9f MD |
515 | if (field_name) { |
516 | if (need_dot) | |
517 | g_string_append(str, "."); | |
b7e35bad | 518 | g_string_append(str, g_quark_to_string(field_name)); |
98df1c9f | 519 | } |
31262354 | 520 | c_str = g_string_free(str, FALSE); |
b7e35bad MD |
521 | if (c_str[0] == '\0') |
522 | return 0; | |
31262354 | 523 | path = g_quark_from_string(c_str); |
98df1c9f | 524 | printf_debug("new definition path: %s\n", c_str); |
31262354 MD |
525 | g_free(c_str); |
526 | return path; | |
527 | } | |
528 | ||
9e29e16e MD |
529 | struct definition_scope * |
530 | new_definition_scope(struct definition_scope *parent_scope, | |
98df1c9f | 531 | GQuark field_name, const char *root_name) |
9e29e16e MD |
532 | { |
533 | struct definition_scope *scope; | |
9e29e16e | 534 | |
98df1c9f MD |
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); | |
9e29e16e | 542 | scope_path_len += parent_scope->scope_path->len; |
98df1c9f MD |
543 | scope = _new_definition_scope(parent_scope, scope_path_len); |
544 | memcpy(scope->scope_path->data, parent_scope->scope_path->data, | |
05c749e5 | 545 | sizeof(GQuark) * (scope_path_len - 1)); |
98df1c9f MD |
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 | } | |
c054553d MD |
558 | return scope; |
559 | } | |
560 | ||
d00d17d1 | 561 | /* |
d60cb676 | 562 | * in: path (dot separated), out: q (GArray of GQuark) |
d00d17d1 | 563 | */ |
d00d17d1 MD |
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); | |
427c09b7 | 586 | ptrend++; /* skip current dot */ |
d00d17d1 MD |
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 | ||
e1151715 | 595 | void free_definition_scope(struct definition_scope *scope) |
c054553d | 596 | { |
05c749e5 | 597 | g_array_free(scope->scope_path, TRUE); |
e1151715 | 598 | g_hash_table_destroy(scope->definitions); |
c054553d | 599 | g_free(scope); |
d1708134 | 600 | } |
a35173fe | 601 | |
a35173fe MD |
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 | } |