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> |
6dc2ca62 | 22 | #include <glib.h> |
d1708134 MD |
23 | #include <errno.h> |
24 | ||
c054553d | 25 | static |
e1151715 | 26 | struct definition * |
f6625916 MD |
27 | lookup_typedef_declaration_scope(GQuark declaration_name, |
28 | struct declaration_scope *scope) | |
d1708134 | 29 | { |
f6625916 MD |
30 | return g_hash_table_lookup(scope->typedef_declarations, |
31 | (gconstpointer) (unsigned long) declaration_name); | |
d1708134 MD |
32 | } |
33 | ||
f6625916 MD |
34 | struct definition *lookup_typedef_declaration(GQuark declaration_name, |
35 | struct declaration_scope *scope) | |
c054553d | 36 | { |
e1151715 | 37 | struct definition *definition; |
c054553d MD |
38 | |
39 | while (scope) { | |
f6625916 MD |
40 | definition = lookup_typedef_declaration_scope(declaration_name, |
41 | scope); | |
e1151715 MD |
42 | if (definition) |
43 | return definition; | |
c054553d MD |
44 | scope = scope->parent_scope; |
45 | } | |
46 | return NULL; | |
47 | } | |
48 | ||
f6625916 MD |
49 | int register_typedef_declaration(GQuark name, struct declaration *declaration, |
50 | struct declaration_scope *scope) | |
d1708134 | 51 | { |
64893f33 | 52 | if (!name) |
6ee5115e MD |
53 | return -EPERM; |
54 | ||
c054553d | 55 | /* Only lookup in local scope */ |
f6625916 | 56 | if (lookup_typedef_declaration_scope(name, scope)) |
d1708134 MD |
57 | return -EEXIST; |
58 | ||
f6625916 | 59 | g_hash_table_insert(scope->typedef_declarations, |
64893f33 | 60 | (gpointer) (unsigned long) name, |
f6625916 MD |
61 | declaration); |
62 | declaration_ref(declaration); | |
ac88af75 MD |
63 | return 0; |
64 | } | |
65 | ||
66 | static | |
e1151715 | 67 | struct definition * |
f6625916 MD |
68 | lookup_field_definition_scope(GQuark field_name, |
69 | struct definition_scope *scope) | |
ac88af75 | 70 | { |
e1151715 | 71 | return g_hash_table_lookup(scope->definitions, |
ac88af75 MD |
72 | (gconstpointer) (unsigned long) field_name); |
73 | } | |
74 | ||
05c749e5 MD |
75 | /* |
76 | * Returns the index at which the paths differ. | |
77 | * If the value returned equals len, it means the paths are identical | |
78 | * from index 0 to len-1. | |
79 | */ | |
80 | static int compare_paths(GArray *a, GArray *b, int len) | |
81 | { | |
82 | int i; | |
83 | ||
84 | assert(len <= a->len); | |
85 | assert(len <= b->len); | |
86 | ||
87 | for (i = 0; i < len; i++) { | |
88 | GQuark qa, qb; | |
89 | ||
90 | qa = g_array_index(a, GQuark, i); | |
91 | qb = g_array_index(b, GQuark, i); | |
92 | if (qa != qb) | |
93 | return i; | |
94 | } | |
95 | return i; | |
96 | } | |
97 | ||
98 | static int is_path_child_of(GArray *path, GArray *maybe_parent) | |
99 | { | |
100 | if (path->len <= maybe_parent->len) | |
101 | return 0; | |
102 | if (compare_paths(path, maybe_parent, maybe_parent->len) | |
103 | == maybe_parent->len) | |
104 | return 1; | |
105 | else | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static struct definition_scope * | |
110 | get_definition_scope(struct definition *definition) | |
111 | { | |
112 | switch (definition->declaration->id) { | |
113 | case CTF_TYPE_STRUCT: | |
114 | { | |
115 | struct definition_struct *def = | |
116 | container_of(definition, struct definition_struct, p); | |
117 | return def->scope; | |
118 | } | |
119 | case CTF_TYPE_VARIANT: | |
120 | { | |
121 | struct definition_variant *def = | |
122 | container_of(definition, struct definition_variant, p); | |
123 | return def->scope; | |
124 | } | |
125 | case CTF_TYPE_ARRAY: | |
126 | { | |
127 | struct definition_array *def = | |
128 | container_of(definition, struct definition_array, p); | |
129 | return def->scope; | |
130 | } | |
131 | case CTF_TYPE_SEQUENCE: | |
132 | { | |
133 | struct definition_sequence *def = | |
134 | container_of(definition, struct definition_sequence, p); | |
135 | return def->scope; | |
136 | } | |
137 | ||
138 | case CTF_TYPE_INTEGER: | |
139 | case CTF_TYPE_FLOAT: | |
140 | case CTF_TYPE_ENUM: | |
141 | case CTF_TYPE_STRING: | |
142 | case CTF_TYPE_UNKNOWN: | |
143 | default: | |
144 | return NULL; | |
145 | } | |
146 | } | |
147 | ||
148 | /* | |
149 | * OK, here is the fun. We want to lookup a field that is: | |
150 | * - either in the current scope, but prior to the current field. | |
151 | * - or in a parent scope (or parent of parent ...) still in a field | |
152 | * prior to the current field position within the parents. | |
153 | * A reaching through a dynamic scoping (e.g. from payload structure to | |
154 | * event header structure), the parent fields are always entirely prior | |
155 | * to the child. | |
156 | * If we cannot find such a field that is prior to our current path, we | |
157 | * return NULL. | |
158 | * | |
159 | * cur_path: the path leading to the variant definition. | |
160 | * lookup_path: the path leading to the enum we want to look for. | |
161 | * scope: the definition scope containing the variant definition. | |
162 | */ | |
e1151715 | 163 | struct definition * |
05c749e5 MD |
164 | lookup_definition(GArray *cur_path, |
165 | GArray *lookup_path, | |
166 | struct definition_scope *scope) | |
ac88af75 | 167 | { |
05c749e5 MD |
168 | struct definition *definition, *lookup_definition; |
169 | GQuark last; | |
170 | int index; | |
ac88af75 MD |
171 | |
172 | while (scope) { | |
05c749e5 MD |
173 | /* going up in the hierarchy. Check where we come from. */ |
174 | assert(is_path_child_of(cur_path, scope->scope_path)); | |
175 | assert(cur_path->len - scope->scope_path->len == 1); | |
176 | last = g_array_index(cur_path, GQuark, cur_path->len - 1); | |
177 | definition = lookup_field_definition_scope(last, scope); | |
178 | assert(definition); | |
179 | index = definition->index; | |
180 | lookup: | |
181 | if (is_path_child_of(lookup_path, scope->scope_path)) { | |
182 | /* Means we can lookup the field in this scope */ | |
183 | last = g_array_index(lookup_path, GQuark, | |
184 | scope->scope_path->len); | |
185 | lookup_definition = lookup_field_definition_scope(last, scope); | |
186 | if (!lookup_definition || ((index != -1) && lookup_definition->index >= index)) | |
187 | return NULL; | |
188 | /* Found it! And it is prior to the current field. */ | |
189 | if (lookup_path->len - scope->scope_path->len == 1) { | |
190 | /* Direct child */ | |
191 | return lookup_definition; | |
192 | } else { | |
193 | scope = get_definition_scope(lookup_definition); | |
194 | /* Check if the definition has a sub-scope */ | |
195 | if (!scope) | |
196 | return NULL; | |
197 | /* | |
198 | * Don't compare index anymore, because we are | |
199 | * going within a scope that has been validated | |
200 | * to be entirely prior to the current scope. | |
201 | */ | |
202 | cur_path = NULL; | |
203 | index = -1; | |
204 | goto lookup; | |
205 | } | |
206 | } else { | |
207 | assert(index != -1); | |
208 | /* lookup_path is within an upper scope */ | |
209 | cur_path = scope->scope_path; | |
210 | scope = scope->parent_scope; | |
211 | } | |
ac88af75 MD |
212 | } |
213 | return NULL; | |
214 | } | |
215 | ||
e1151715 | 216 | int register_field_definition(GQuark field_name, struct definition *definition, |
f6625916 | 217 | struct definition_scope *scope) |
ac88af75 MD |
218 | { |
219 | if (!field_name) | |
220 | return -EPERM; | |
221 | ||
222 | /* Only lookup in local scope */ | |
e1151715 | 223 | if (lookup_field_definition_scope(field_name, scope)) |
ac88af75 MD |
224 | return -EEXIST; |
225 | ||
e1151715 | 226 | g_hash_table_insert(scope->definitions, |
ac88af75 | 227 | (gpointer) (unsigned long) field_name, |
e1151715 MD |
228 | definition); |
229 | definition_ref(definition); | |
d1708134 MD |
230 | return 0; |
231 | } | |
232 | ||
f6625916 | 233 | void declaration_ref(struct declaration *declaration) |
4c8bfb7e | 234 | { |
f6625916 | 235 | declaration->ref++; |
4c8bfb7e MD |
236 | } |
237 | ||
f6625916 | 238 | void declaration_unref(struct declaration *declaration) |
4c8bfb7e | 239 | { |
f6625916 MD |
240 | if (!--declaration->ref) |
241 | declaration->declaration_free(declaration); | |
4c8bfb7e MD |
242 | } |
243 | ||
e1151715 | 244 | void definition_ref(struct definition *definition) |
d1708134 | 245 | { |
e1151715 | 246 | definition->ref++; |
d1708134 MD |
247 | } |
248 | ||
e1151715 | 249 | void definition_unref(struct definition *definition) |
d1708134 | 250 | { |
e1151715 | 251 | if (!--definition->ref) |
f6625916 | 252 | definition->declaration->definition_free(definition); |
c054553d MD |
253 | } |
254 | ||
f6625916 MD |
255 | struct declaration_scope * |
256 | new_declaration_scope(struct declaration_scope *parent_scope) | |
c054553d | 257 | { |
f6625916 | 258 | struct declaration_scope *scope = g_new(struct declaration_scope, 1); |
c054553d | 259 | |
f6625916 | 260 | scope->typedef_declarations = g_hash_table_new_full(g_direct_hash, |
c13cbf74 | 261 | g_direct_equal, NULL, |
e1151715 | 262 | (GDestroyNotify) definition_unref); |
f6625916 | 263 | scope->struct_declarations = g_hash_table_new_full(g_direct_hash, |
c13cbf74 | 264 | g_direct_equal, NULL, |
f6625916 MD |
265 | (GDestroyNotify) declaration_unref); |
266 | scope->variant_declarations = g_hash_table_new_full(g_direct_hash, | |
c13cbf74 | 267 | g_direct_equal, NULL, |
f6625916 MD |
268 | (GDestroyNotify) declaration_unref); |
269 | scope->enum_declarations = g_hash_table_new_full(g_direct_hash, | |
c054553d | 270 | g_direct_equal, NULL, |
f6625916 | 271 | (GDestroyNotify) declaration_unref); |
64893f33 MD |
272 | scope->parent_scope = parent_scope; |
273 | return scope; | |
274 | } | |
275 | ||
f6625916 | 276 | void free_declaration_scope(struct declaration_scope *scope) |
64893f33 | 277 | { |
f6625916 MD |
278 | g_hash_table_destroy(scope->enum_declarations); |
279 | g_hash_table_destroy(scope->variant_declarations); | |
280 | g_hash_table_destroy(scope->struct_declarations); | |
281 | g_hash_table_destroy(scope->typedef_declarations); | |
64893f33 MD |
282 | g_free(scope); |
283 | } | |
284 | ||
c13cbf74 | 285 | static |
f6625916 MD |
286 | struct declaration_struct *lookup_struct_declaration_scope(GQuark struct_name, |
287 | struct declaration_scope *scope) | |
c13cbf74 | 288 | { |
f6625916 | 289 | return g_hash_table_lookup(scope->struct_declarations, |
c13cbf74 MD |
290 | (gconstpointer) (unsigned long) struct_name); |
291 | } | |
292 | ||
f6625916 MD |
293 | struct declaration_struct *lookup_struct_declaration(GQuark struct_name, |
294 | struct declaration_scope *scope) | |
c13cbf74 | 295 | { |
f6625916 | 296 | struct declaration_struct *declaration; |
c13cbf74 MD |
297 | |
298 | while (scope) { | |
f6625916 MD |
299 | declaration = lookup_struct_declaration_scope(struct_name, scope); |
300 | if (declaration) | |
301 | return declaration; | |
c13cbf74 MD |
302 | scope = scope->parent_scope; |
303 | } | |
304 | return NULL; | |
305 | } | |
306 | ||
f6625916 MD |
307 | int register_struct_declaration(GQuark struct_name, |
308 | struct declaration_struct *struct_declaration, | |
309 | struct declaration_scope *scope) | |
c13cbf74 MD |
310 | { |
311 | if (!struct_name) | |
312 | return -EPERM; | |
313 | ||
314 | /* Only lookup in local scope */ | |
f6625916 | 315 | if (lookup_struct_declaration_scope(struct_name, scope)) |
c13cbf74 MD |
316 | return -EEXIST; |
317 | ||
f6625916 | 318 | g_hash_table_insert(scope->struct_declarations, |
c13cbf74 | 319 | (gpointer) (unsigned long) struct_name, |
f6625916 MD |
320 | struct_declaration); |
321 | declaration_ref(&struct_declaration->p); | |
c13cbf74 MD |
322 | return 0; |
323 | } | |
324 | ||
325 | static | |
f6625916 MD |
326 | struct declaration_variant * |
327 | lookup_variant_declaration_scope(GQuark variant_name, | |
328 | struct declaration_scope *scope) | |
c13cbf74 | 329 | { |
f6625916 | 330 | return g_hash_table_lookup(scope->variant_declarations, |
c13cbf74 MD |
331 | (gconstpointer) (unsigned long) variant_name); |
332 | } | |
333 | ||
f6625916 MD |
334 | struct declaration_variant * |
335 | lookup_variant_declaration(GQuark variant_name, | |
336 | struct declaration_scope *scope) | |
c13cbf74 | 337 | { |
f6625916 | 338 | struct declaration_variant *declaration; |
c13cbf74 MD |
339 | |
340 | while (scope) { | |
f6625916 MD |
341 | declaration = lookup_variant_declaration_scope(variant_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_variant_declaration(GQuark variant_name, |
350 | struct declaration_variant *variant_declaration, | |
351 | struct declaration_scope *scope) | |
c13cbf74 MD |
352 | { |
353 | if (!variant_name) | |
354 | return -EPERM; | |
355 | ||
356 | /* Only lookup in local scope */ | |
f6625916 | 357 | if (lookup_variant_declaration_scope(variant_name, scope)) |
c13cbf74 MD |
358 | return -EEXIST; |
359 | ||
f6625916 | 360 | g_hash_table_insert(scope->variant_declarations, |
c13cbf74 | 361 | (gpointer) (unsigned long) variant_name, |
f6625916 MD |
362 | variant_declaration); |
363 | declaration_ref(&variant_declaration->p); | |
c13cbf74 MD |
364 | return 0; |
365 | } | |
366 | ||
367 | static | |
f6625916 MD |
368 | struct declaration_enum * |
369 | lookup_enum_declaration_scope(GQuark enum_name, | |
370 | struct declaration_scope *scope) | |
c13cbf74 | 371 | { |
f6625916 | 372 | return g_hash_table_lookup(scope->enum_declarations, |
c13cbf74 MD |
373 | (gconstpointer) (unsigned long) enum_name); |
374 | } | |
375 | ||
f6625916 MD |
376 | struct declaration_enum * |
377 | lookup_enum_declaration(GQuark enum_name, | |
378 | struct declaration_scope *scope) | |
c13cbf74 | 379 | { |
f6625916 | 380 | struct declaration_enum *declaration; |
c13cbf74 MD |
381 | |
382 | while (scope) { | |
f6625916 MD |
383 | declaration = lookup_enum_declaration_scope(enum_name, scope); |
384 | if (declaration) | |
385 | return declaration; | |
c13cbf74 MD |
386 | scope = scope->parent_scope; |
387 | } | |
388 | return NULL; | |
389 | } | |
390 | ||
f6625916 MD |
391 | int register_enum_declaration(GQuark enum_name, |
392 | struct declaration_enum *enum_declaration, | |
393 | struct declaration_scope *scope) | |
c13cbf74 MD |
394 | { |
395 | if (!enum_name) | |
396 | return -EPERM; | |
397 | ||
398 | /* Only lookup in local scope */ | |
f6625916 | 399 | if (lookup_enum_declaration_scope(enum_name, scope)) |
c13cbf74 MD |
400 | return -EEXIST; |
401 | ||
f6625916 | 402 | g_hash_table_insert(scope->enum_declarations, |
c13cbf74 | 403 | (gpointer) (unsigned long) enum_name, |
f6625916 MD |
404 | enum_declaration); |
405 | declaration_ref(&enum_declaration->p); | |
c13cbf74 MD |
406 | return 0; |
407 | } | |
408 | ||
e1151715 | 409 | struct definition_scope * |
05c749e5 MD |
410 | new_definition_scope(struct definition_scope *parent_scope, |
411 | GQuark field_name) | |
64893f33 | 412 | { |
e1151715 | 413 | struct definition_scope *scope = g_new(struct definition_scope, 1); |
05c749e5 | 414 | int scope_path_len = 1; |
64893f33 | 415 | |
e1151715 | 416 | scope->definitions = g_hash_table_new_full(g_direct_hash, |
ac88af75 | 417 | g_direct_equal, NULL, |
e1151715 | 418 | (GDestroyNotify) definition_unref); |
c054553d | 419 | scope->parent_scope = parent_scope; |
05c749e5 MD |
420 | if (scope->parent_scope) |
421 | scope_path_len += scope->parent_scope->scope_path->len; | |
422 | scope->scope_path = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), | |
423 | scope_path_len); | |
424 | g_array_set_size(scope->scope_path, scope_path_len); | |
425 | if (scope->parent_scope) | |
426 | memcpy(scope->scope_path, scope->parent_scope->scope_path, | |
427 | sizeof(GQuark) * (scope_path_len - 1)); | |
428 | g_array_index(scope->scope_path, GQuark, scope_path_len - 1) = | |
429 | field_name; | |
c054553d MD |
430 | return scope; |
431 | } | |
432 | ||
e1151715 | 433 | void free_definition_scope(struct definition_scope *scope) |
c054553d | 434 | { |
05c749e5 | 435 | g_array_free(scope->scope_path, TRUE); |
e1151715 | 436 | g_hash_table_destroy(scope->definitions); |
c054553d | 437 | g_free(scope); |
d1708134 | 438 | } |