Commit | Line | Data |
---|---|---|
6dc2ca62 | 1 | /* |
f6625916 | 2 | * declarations.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> |
6a36ddca | 22 | #include <limits.h> |
6dc2ca62 | 23 | #include <glib.h> |
d1708134 MD |
24 | #include <errno.h> |
25 | ||
c054553d | 26 | static |
78af2bcd MD |
27 | GQuark prefix_quark(const char *prefix, GQuark quark) |
28 | { | |
29 | GQuark nq; | |
30 | GString *str; | |
31 | ||
32 | str = g_string_new(prefix); | |
33 | g_string_append(str, g_quark_to_string(quark)); | |
34 | nq = g_quark_from_string(g_string_free(str, FALSE)); | |
35 | return nq; | |
36 | } | |
37 | ||
38 | static | |
39 | struct declaration * | |
40 | lookup_declaration_scope(GQuark declaration_name, | |
f6625916 | 41 | struct declaration_scope *scope) |
d1708134 | 42 | { |
f6625916 MD |
43 | return g_hash_table_lookup(scope->typedef_declarations, |
44 | (gconstpointer) (unsigned long) declaration_name); | |
d1708134 MD |
45 | } |
46 | ||
78af2bcd | 47 | struct declaration *lookup_declaration(GQuark declaration_name, |
f6625916 | 48 | struct declaration_scope *scope) |
c054553d | 49 | { |
78af2bcd | 50 | struct declaration *declaration; |
c054553d MD |
51 | |
52 | while (scope) { | |
78af2bcd MD |
53 | declaration = lookup_declaration_scope(declaration_name, |
54 | scope); | |
55 | if (declaration) | |
56 | return declaration; | |
c054553d MD |
57 | scope = scope->parent_scope; |
58 | } | |
59 | return NULL; | |
60 | } | |
61 | ||
78af2bcd | 62 | int register_declaration(GQuark name, struct declaration *declaration, |
f6625916 | 63 | struct declaration_scope *scope) |
d1708134 | 64 | { |
64893f33 | 65 | if (!name) |
6ee5115e MD |
66 | return -EPERM; |
67 | ||
c054553d | 68 | /* Only lookup in local scope */ |
78af2bcd | 69 | if (lookup_declaration_scope(name, scope)) |
d1708134 MD |
70 | return -EEXIST; |
71 | ||
f6625916 | 72 | g_hash_table_insert(scope->typedef_declarations, |
64893f33 | 73 | (gpointer) (unsigned long) name, |
f6625916 MD |
74 | declaration); |
75 | declaration_ref(declaration); | |
ac88af75 MD |
76 | return 0; |
77 | } | |
78 | ||
79 | static | |
e1151715 | 80 | struct definition * |
f6625916 MD |
81 | lookup_field_definition_scope(GQuark field_name, |
82 | struct definition_scope *scope) | |
ac88af75 | 83 | { |
e1151715 | 84 | return g_hash_table_lookup(scope->definitions, |
ac88af75 MD |
85 | (gconstpointer) (unsigned long) field_name); |
86 | } | |
87 | ||
05c749e5 MD |
88 | /* |
89 | * Returns the index at which the paths differ. | |
90 | * If the value returned equals len, it means the paths are identical | |
91 | * from index 0 to len-1. | |
92 | */ | |
93 | static int compare_paths(GArray *a, GArray *b, int len) | |
94 | { | |
95 | int i; | |
96 | ||
97 | assert(len <= a->len); | |
98 | assert(len <= b->len); | |
99 | ||
100 | for (i = 0; i < len; i++) { | |
101 | GQuark qa, qb; | |
102 | ||
103 | qa = g_array_index(a, GQuark, i); | |
104 | qb = g_array_index(b, GQuark, i); | |
105 | if (qa != qb) | |
106 | return i; | |
107 | } | |
108 | return i; | |
109 | } | |
110 | ||
111 | static int is_path_child_of(GArray *path, GArray *maybe_parent) | |
112 | { | |
113 | if (path->len <= maybe_parent->len) | |
114 | return 0; | |
115 | if (compare_paths(path, maybe_parent, maybe_parent->len) | |
116 | == maybe_parent->len) | |
117 | return 1; | |
118 | else | |
119 | return 0; | |
120 | } | |
121 | ||
122 | static struct definition_scope * | |
123 | get_definition_scope(struct definition *definition) | |
124 | { | |
125 | switch (definition->declaration->id) { | |
126 | case CTF_TYPE_STRUCT: | |
127 | { | |
128 | struct definition_struct *def = | |
129 | container_of(definition, struct definition_struct, p); | |
130 | return def->scope; | |
131 | } | |
132 | case CTF_TYPE_VARIANT: | |
133 | { | |
134 | struct definition_variant *def = | |
135 | container_of(definition, struct definition_variant, p); | |
136 | return def->scope; | |
137 | } | |
138 | case CTF_TYPE_ARRAY: | |
139 | { | |
140 | struct definition_array *def = | |
141 | container_of(definition, struct definition_array, p); | |
142 | return def->scope; | |
143 | } | |
144 | case CTF_TYPE_SEQUENCE: | |
145 | { | |
146 | struct definition_sequence *def = | |
147 | container_of(definition, struct definition_sequence, p); | |
148 | return def->scope; | |
149 | } | |
150 | ||
151 | case CTF_TYPE_INTEGER: | |
152 | case CTF_TYPE_FLOAT: | |
153 | case CTF_TYPE_ENUM: | |
154 | case CTF_TYPE_STRING: | |
155 | case CTF_TYPE_UNKNOWN: | |
156 | default: | |
157 | return NULL; | |
158 | } | |
159 | } | |
160 | ||
161 | /* | |
162 | * OK, here is the fun. We want to lookup a field that is: | |
9e29e16e MD |
163 | * - either in the same dynamic scope: |
164 | * - either in the current scope, but prior to the current field. | |
165 | * - or in a parent scope (or parent of parent ...) still in a field | |
166 | * prior to the current field position within the parents. | |
167 | * - or in a different dynamic scope: | |
168 | * - either in a upper dynamic scope (walk down a targeted scope from | |
169 | * the dynamic scope root) | |
170 | * - or in a lower dynamic scope (failure) | |
171 | * The dynamic scope roots are linked together, so we can access the | |
172 | * parent dynamic scope from the child dynamic scope by walking up to | |
173 | * the parent. | |
05c749e5 MD |
174 | * If we cannot find such a field that is prior to our current path, we |
175 | * return NULL. | |
176 | * | |
177 | * cur_path: the path leading to the variant definition. | |
178 | * lookup_path: the path leading to the enum we want to look for. | |
179 | * scope: the definition scope containing the variant definition. | |
180 | */ | |
e1151715 | 181 | struct definition * |
05c749e5 MD |
182 | lookup_definition(GArray *cur_path, |
183 | GArray *lookup_path, | |
184 | struct definition_scope *scope) | |
ac88af75 | 185 | { |
05c749e5 MD |
186 | struct definition *definition, *lookup_definition; |
187 | GQuark last; | |
188 | int index; | |
ac88af75 MD |
189 | |
190 | while (scope) { | |
05c749e5 MD |
191 | /* going up in the hierarchy. Check where we come from. */ |
192 | assert(is_path_child_of(cur_path, scope->scope_path)); | |
193 | assert(cur_path->len - scope->scope_path->len == 1); | |
194 | last = g_array_index(cur_path, GQuark, cur_path->len - 1); | |
195 | definition = lookup_field_definition_scope(last, scope); | |
196 | assert(definition); | |
197 | index = definition->index; | |
198 | lookup: | |
199 | if (is_path_child_of(lookup_path, scope->scope_path)) { | |
200 | /* Means we can lookup the field in this scope */ | |
201 | last = g_array_index(lookup_path, GQuark, | |
202 | scope->scope_path->len); | |
203 | lookup_definition = lookup_field_definition_scope(last, scope); | |
204 | if (!lookup_definition || ((index != -1) && lookup_definition->index >= index)) | |
205 | return NULL; | |
206 | /* Found it! And it is prior to the current field. */ | |
207 | if (lookup_path->len - scope->scope_path->len == 1) { | |
208 | /* Direct child */ | |
209 | return lookup_definition; | |
210 | } else { | |
211 | scope = get_definition_scope(lookup_definition); | |
212 | /* Check if the definition has a sub-scope */ | |
213 | if (!scope) | |
214 | return NULL; | |
215 | /* | |
216 | * Don't compare index anymore, because we are | |
217 | * going within a scope that has been validated | |
218 | * to be entirely prior to the current scope. | |
219 | */ | |
220 | cur_path = NULL; | |
221 | index = -1; | |
222 | goto lookup; | |
223 | } | |
224 | } else { | |
225 | assert(index != -1); | |
226 | /* lookup_path is within an upper scope */ | |
227 | cur_path = scope->scope_path; | |
228 | scope = scope->parent_scope; | |
229 | } | |
ac88af75 MD |
230 | } |
231 | return NULL; | |
232 | } | |
233 | ||
e1151715 | 234 | int register_field_definition(GQuark field_name, struct definition *definition, |
f6625916 | 235 | struct definition_scope *scope) |
ac88af75 MD |
236 | { |
237 | if (!field_name) | |
238 | return -EPERM; | |
239 | ||
240 | /* Only lookup in local scope */ | |
e1151715 | 241 | if (lookup_field_definition_scope(field_name, scope)) |
ac88af75 MD |
242 | return -EEXIST; |
243 | ||
e1151715 | 244 | g_hash_table_insert(scope->definitions, |
ac88af75 | 245 | (gpointer) (unsigned long) field_name, |
e1151715 MD |
246 | definition); |
247 | definition_ref(definition); | |
d1708134 MD |
248 | return 0; |
249 | } | |
250 | ||
f6625916 | 251 | void declaration_ref(struct declaration *declaration) |
4c8bfb7e | 252 | { |
f6625916 | 253 | declaration->ref++; |
4c8bfb7e MD |
254 | } |
255 | ||
f6625916 | 256 | void declaration_unref(struct declaration *declaration) |
4c8bfb7e | 257 | { |
ff00cad2 MD |
258 | if (!declaration) |
259 | return; | |
f6625916 MD |
260 | if (!--declaration->ref) |
261 | declaration->declaration_free(declaration); | |
4c8bfb7e MD |
262 | } |
263 | ||
e1151715 | 264 | void definition_ref(struct definition *definition) |
d1708134 | 265 | { |
e1151715 | 266 | definition->ref++; |
d1708134 MD |
267 | } |
268 | ||
e1151715 | 269 | void definition_unref(struct definition *definition) |
d1708134 | 270 | { |
ff00cad2 MD |
271 | if (!definition) |
272 | return; | |
e1151715 | 273 | if (!--definition->ref) |
f6625916 | 274 | definition->declaration->definition_free(definition); |
c054553d MD |
275 | } |
276 | ||
f6625916 MD |
277 | struct declaration_scope * |
278 | new_declaration_scope(struct declaration_scope *parent_scope) | |
c054553d | 279 | { |
f6625916 | 280 | struct declaration_scope *scope = g_new(struct declaration_scope, 1); |
c054553d | 281 | |
f6625916 | 282 | scope->typedef_declarations = g_hash_table_new_full(g_direct_hash, |
c13cbf74 | 283 | g_direct_equal, NULL, |
e1151715 | 284 | (GDestroyNotify) definition_unref); |
f6625916 | 285 | scope->struct_declarations = g_hash_table_new_full(g_direct_hash, |
c13cbf74 | 286 | g_direct_equal, NULL, |
f6625916 MD |
287 | (GDestroyNotify) declaration_unref); |
288 | scope->variant_declarations = g_hash_table_new_full(g_direct_hash, | |
c13cbf74 | 289 | g_direct_equal, NULL, |
f6625916 MD |
290 | (GDestroyNotify) declaration_unref); |
291 | scope->enum_declarations = g_hash_table_new_full(g_direct_hash, | |
c054553d | 292 | g_direct_equal, NULL, |
f6625916 | 293 | (GDestroyNotify) declaration_unref); |
64893f33 MD |
294 | scope->parent_scope = parent_scope; |
295 | return scope; | |
296 | } | |
297 | ||
f6625916 | 298 | void free_declaration_scope(struct declaration_scope *scope) |
64893f33 | 299 | { |
f6625916 MD |
300 | g_hash_table_destroy(scope->enum_declarations); |
301 | g_hash_table_destroy(scope->variant_declarations); | |
302 | g_hash_table_destroy(scope->struct_declarations); | |
303 | g_hash_table_destroy(scope->typedef_declarations); | |
64893f33 MD |
304 | g_free(scope); |
305 | } | |
306 | ||
c13cbf74 | 307 | static |
f6625916 MD |
308 | struct declaration_struct *lookup_struct_declaration_scope(GQuark struct_name, |
309 | struct declaration_scope *scope) | |
c13cbf74 | 310 | { |
f6625916 | 311 | return g_hash_table_lookup(scope->struct_declarations, |
c13cbf74 MD |
312 | (gconstpointer) (unsigned long) struct_name); |
313 | } | |
314 | ||
f6625916 MD |
315 | struct declaration_struct *lookup_struct_declaration(GQuark struct_name, |
316 | struct declaration_scope *scope) | |
c13cbf74 | 317 | { |
f6625916 | 318 | struct declaration_struct *declaration; |
c13cbf74 MD |
319 | |
320 | while (scope) { | |
f6625916 MD |
321 | declaration = lookup_struct_declaration_scope(struct_name, scope); |
322 | if (declaration) | |
323 | return declaration; | |
c13cbf74 MD |
324 | scope = scope->parent_scope; |
325 | } | |
326 | return NULL; | |
327 | } | |
328 | ||
f6625916 MD |
329 | int register_struct_declaration(GQuark struct_name, |
330 | struct declaration_struct *struct_declaration, | |
331 | struct declaration_scope *scope) | |
c13cbf74 | 332 | { |
78af2bcd MD |
333 | GQuark prefix_name; |
334 | int ret; | |
335 | ||
c13cbf74 MD |
336 | if (!struct_name) |
337 | return -EPERM; | |
338 | ||
339 | /* Only lookup in local scope */ | |
f6625916 | 340 | if (lookup_struct_declaration_scope(struct_name, scope)) |
c13cbf74 MD |
341 | return -EEXIST; |
342 | ||
f6625916 | 343 | g_hash_table_insert(scope->struct_declarations, |
c13cbf74 | 344 | (gpointer) (unsigned long) struct_name, |
f6625916 MD |
345 | struct_declaration); |
346 | declaration_ref(&struct_declaration->p); | |
78af2bcd MD |
347 | |
348 | /* Also add in typedef/typealias scopes */ | |
349 | prefix_name = prefix_quark("struct ", struct_name); | |
350 | ret = register_declaration(prefix_name, &struct_declaration->p, scope); | |
351 | assert(!ret); | |
c13cbf74 MD |
352 | return 0; |
353 | } | |
354 | ||
355 | static | |
a0720417 | 356 | struct declaration_untagged_variant * |
f6625916 MD |
357 | lookup_variant_declaration_scope(GQuark variant_name, |
358 | struct declaration_scope *scope) | |
c13cbf74 | 359 | { |
f6625916 | 360 | return g_hash_table_lookup(scope->variant_declarations, |
c13cbf74 MD |
361 | (gconstpointer) (unsigned long) variant_name); |
362 | } | |
363 | ||
a0720417 | 364 | struct declaration_untagged_variant * |
f6625916 MD |
365 | lookup_variant_declaration(GQuark variant_name, |
366 | struct declaration_scope *scope) | |
c13cbf74 | 367 | { |
a0720417 | 368 | struct declaration_untagged_variant *declaration; |
c13cbf74 MD |
369 | |
370 | while (scope) { | |
f6625916 MD |
371 | declaration = lookup_variant_declaration_scope(variant_name, scope); |
372 | if (declaration) | |
373 | return declaration; | |
c13cbf74 MD |
374 | scope = scope->parent_scope; |
375 | } | |
376 | return NULL; | |
377 | } | |
378 | ||
f6625916 | 379 | int register_variant_declaration(GQuark variant_name, |
a0720417 | 380 | struct declaration_untagged_variant *untagged_variant_declaration, |
f6625916 | 381 | struct declaration_scope *scope) |
c13cbf74 | 382 | { |
78af2bcd MD |
383 | GQuark prefix_name; |
384 | int ret; | |
385 | ||
c13cbf74 MD |
386 | if (!variant_name) |
387 | return -EPERM; | |
388 | ||
389 | /* Only lookup in local scope */ | |
f6625916 | 390 | if (lookup_variant_declaration_scope(variant_name, scope)) |
c13cbf74 MD |
391 | return -EEXIST; |
392 | ||
f6625916 | 393 | g_hash_table_insert(scope->variant_declarations, |
c13cbf74 | 394 | (gpointer) (unsigned long) variant_name, |
a0720417 MD |
395 | untagged_variant_declaration); |
396 | declaration_ref(&untagged_variant_declaration->p); | |
78af2bcd MD |
397 | |
398 | /* Also add in typedef/typealias scopes */ | |
399 | prefix_name = prefix_quark("variant ", variant_name); | |
400 | ret = register_declaration(prefix_name, | |
401 | &untagged_variant_declaration->p, scope); | |
402 | assert(!ret); | |
c13cbf74 MD |
403 | return 0; |
404 | } | |
405 | ||
406 | static | |
f6625916 MD |
407 | struct declaration_enum * |
408 | lookup_enum_declaration_scope(GQuark enum_name, | |
409 | struct declaration_scope *scope) | |
c13cbf74 | 410 | { |
f6625916 | 411 | return g_hash_table_lookup(scope->enum_declarations, |
c13cbf74 MD |
412 | (gconstpointer) (unsigned long) enum_name); |
413 | } | |
414 | ||
f6625916 MD |
415 | struct declaration_enum * |
416 | lookup_enum_declaration(GQuark enum_name, | |
417 | struct declaration_scope *scope) | |
c13cbf74 | 418 | { |
f6625916 | 419 | struct declaration_enum *declaration; |
c13cbf74 MD |
420 | |
421 | while (scope) { | |
f6625916 MD |
422 | declaration = lookup_enum_declaration_scope(enum_name, scope); |
423 | if (declaration) | |
424 | return declaration; | |
c13cbf74 MD |
425 | scope = scope->parent_scope; |
426 | } | |
427 | return NULL; | |
428 | } | |
429 | ||
f6625916 MD |
430 | int register_enum_declaration(GQuark enum_name, |
431 | struct declaration_enum *enum_declaration, | |
432 | struct declaration_scope *scope) | |
c13cbf74 | 433 | { |
78af2bcd MD |
434 | GQuark prefix_name; |
435 | int ret; | |
436 | ||
c13cbf74 MD |
437 | if (!enum_name) |
438 | return -EPERM; | |
439 | ||
440 | /* Only lookup in local scope */ | |
f6625916 | 441 | if (lookup_enum_declaration_scope(enum_name, scope)) |
c13cbf74 MD |
442 | return -EEXIST; |
443 | ||
f6625916 | 444 | g_hash_table_insert(scope->enum_declarations, |
c13cbf74 | 445 | (gpointer) (unsigned long) enum_name, |
f6625916 MD |
446 | enum_declaration); |
447 | declaration_ref(&enum_declaration->p); | |
78af2bcd MD |
448 | |
449 | /* Also add in typedef/typealias scopes */ | |
450 | prefix_name = prefix_quark("enum ", enum_name); | |
451 | ret = register_declaration(prefix_name, &enum_declaration->p, scope); | |
452 | assert(!ret); | |
c13cbf74 MD |
453 | return 0; |
454 | } | |
455 | ||
9e29e16e MD |
456 | static struct definition_scope * |
457 | _new_definition_scope(struct definition_scope *parent_scope, | |
458 | int scope_path_len) | |
64893f33 | 459 | { |
e1151715 | 460 | struct definition_scope *scope = g_new(struct definition_scope, 1); |
64893f33 | 461 | |
e1151715 | 462 | scope->definitions = g_hash_table_new_full(g_direct_hash, |
ac88af75 | 463 | g_direct_equal, NULL, |
e1151715 | 464 | (GDestroyNotify) definition_unref); |
c054553d | 465 | scope->parent_scope = parent_scope; |
05c749e5 MD |
466 | scope->scope_path = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), |
467 | scope_path_len); | |
468 | g_array_set_size(scope->scope_path, scope_path_len); | |
9e29e16e MD |
469 | return scope; |
470 | } | |
471 | ||
472 | struct definition_scope * | |
473 | new_definition_scope(struct definition_scope *parent_scope, | |
474 | GQuark field_name) | |
475 | { | |
476 | struct definition_scope *scope; | |
477 | int scope_path_len = 1; | |
478 | ||
479 | if (parent_scope) | |
480 | scope_path_len += parent_scope->scope_path->len; | |
481 | scope = _new_definition_scope(parent_scope, scope_path_len); | |
482 | if (parent_scope) | |
483 | memcpy(scope->scope_path, parent_scope->scope_path, | |
05c749e5 MD |
484 | sizeof(GQuark) * (scope_path_len - 1)); |
485 | g_array_index(scope->scope_path, GQuark, scope_path_len - 1) = | |
486 | field_name; | |
c054553d MD |
487 | return scope; |
488 | } | |
489 | ||
d00d17d1 | 490 | /* |
d60cb676 | 491 | * in: path (dot separated), out: q (GArray of GQuark) |
d00d17d1 | 492 | */ |
d00d17d1 MD |
493 | void append_scope_path(const char *path, GArray *q) |
494 | { | |
495 | const char *ptrbegin, *ptrend = path; | |
496 | GQuark quark; | |
497 | ||
498 | for (;;) { | |
499 | char *str; | |
500 | size_t len; | |
501 | ||
502 | ptrbegin = ptrend; | |
503 | ptrend = strchr(ptrbegin, '.'); | |
504 | if (!ptrend) | |
505 | break; | |
506 | len = ptrend - ptrbegin; | |
507 | /* Don't accept two consecutive dots */ | |
508 | assert(len != 0); | |
509 | str = g_new(char, len + 1); /* include \0 */ | |
510 | memcpy(str, ptrbegin, len); | |
511 | str[len] = '\0'; | |
512 | quark = g_quark_from_string(str); | |
513 | g_array_append_val(q, quark); | |
514 | g_free(str); | |
427c09b7 | 515 | ptrend++; /* skip current dot */ |
d00d17d1 MD |
516 | } |
517 | /* last. Check for trailing dot (and discard). */ | |
518 | if (ptrbegin[0] != '\0') { | |
519 | quark = g_quark_from_string(ptrbegin); | |
520 | g_array_append_val(q, quark); | |
521 | } | |
522 | } | |
523 | ||
6a36ddca MD |
524 | void set_dynamic_definition_scope(struct definition *definition, |
525 | struct definition_scope *scope, | |
d00d17d1 | 526 | const char *root_name) |
9e29e16e | 527 | { |
d00d17d1 MD |
528 | g_array_set_size(scope->scope_path, 0); |
529 | append_scope_path(root_name, scope->scope_path); | |
6a36ddca MD |
530 | /* |
531 | * Use INT_MAX order to ensure that all fields of the parent | |
532 | * scope are seen as being prior to this scope. | |
533 | */ | |
534 | definition->index = INT_MAX; | |
9e29e16e MD |
535 | } |
536 | ||
e1151715 | 537 | void free_definition_scope(struct definition_scope *scope) |
c054553d | 538 | { |
05c749e5 | 539 | g_array_free(scope->scope_path, TRUE); |
e1151715 | 540 | g_hash_table_destroy(scope->definitions); |
c054553d | 541 | g_free(scope); |
d1708134 | 542 | } |