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