Merge branch 'master' of ssh://efficios.com/home/efficios/git/babeltrace
[babeltrace.git] / types / variant.c
1 /*
2 * variant.c
3 *
4 * BabelTrace - Variant Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
23 #include <errno.h>
24
25 static
26 struct definition *_variant_definition_new(struct declaration *declaration,
27 struct definition_scope *parent_scope,
28 GQuark field_name, int index,
29 const char *root_name);
30 static
31 void _variant_definition_free(struct definition *definition);
32
33 int variant_rw(struct stream_pos *ppos, struct definition *definition)
34 {
35 struct definition_variant *variant_definition =
36 container_of(definition, struct definition_variant, p);
37 struct definition *field;
38
39 field = variant_get_current_field(variant_definition);
40 return generic_rw(ppos, field);
41 }
42
43 static
44 void _untagged_variant_declaration_free(struct declaration *declaration)
45 {
46 struct declaration_untagged_variant *untagged_variant_declaration =
47 container_of(declaration, struct declaration_untagged_variant, p);
48 unsigned long i;
49
50 free_declaration_scope(untagged_variant_declaration->scope);
51 g_hash_table_destroy(untagged_variant_declaration->fields_by_tag);
52
53 for (i = 0; i < untagged_variant_declaration->fields->len; i++) {
54 struct declaration_field *declaration_field =
55 &g_array_index(untagged_variant_declaration->fields,
56 struct declaration_field, i);
57 declaration_unref(declaration_field->declaration);
58 }
59 g_array_free(untagged_variant_declaration->fields, true);
60 g_free(untagged_variant_declaration);
61 }
62
63 struct declaration_untagged_variant *untagged_variant_declaration_new(
64 struct declaration_scope *parent_scope)
65 {
66 struct declaration_untagged_variant *untagged_variant_declaration;
67 struct declaration *declaration;
68
69 untagged_variant_declaration = g_new(struct declaration_untagged_variant, 1);
70 declaration = &untagged_variant_declaration->p;
71 untagged_variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
72 g_direct_equal);
73 untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
74 sizeof(struct declaration_field),
75 DEFAULT_NR_STRUCT_FIELDS);
76 untagged_variant_declaration->scope = new_declaration_scope(parent_scope);
77 declaration->id = CTF_TYPE_UNTAGGED_VARIANT;
78 declaration->alignment = 1;
79 declaration->declaration_free = _untagged_variant_declaration_free;
80 declaration->definition_new = NULL;
81 declaration->definition_free = NULL;
82 declaration->ref = 1;
83 return untagged_variant_declaration;
84 }
85
86 static
87 void _variant_declaration_free(struct declaration *declaration)
88 {
89 struct declaration_variant *variant_declaration =
90 container_of(declaration, struct declaration_variant, p);
91
92 _untagged_variant_declaration_free(&variant_declaration->untagged_variant->p);
93 g_array_free(variant_declaration->tag_name, TRUE);
94 }
95
96 struct declaration_variant *
97 variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
98 {
99 struct declaration_variant *variant_declaration;
100 struct declaration *declaration;
101
102 variant_declaration = g_new(struct declaration_variant, 1);
103 declaration = &variant_declaration->p;
104 variant_declaration->untagged_variant = untagged_variant;
105 declaration_ref(&untagged_variant->p);
106 variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
107 append_scope_path(tag, variant_declaration->tag_name);
108 declaration->id = CTF_TYPE_VARIANT;
109 declaration->alignment = 1;
110 declaration->declaration_free = _variant_declaration_free;
111 declaration->definition_new = _variant_definition_new;
112 declaration->definition_free = _variant_definition_free;
113 declaration->ref = 1;
114 return variant_declaration;
115 }
116
117 /*
118 * tag_instance is assumed to be an enumeration.
119 * Returns 0 if OK, < 0 if error.
120 */
121 static
122 int check_enum_tag(struct definition_variant *variant,
123 struct definition *enum_tag)
124 {
125 struct definition_enum *_enum =
126 container_of(enum_tag, struct definition_enum, p);
127 struct declaration_enum *enum_declaration = _enum->declaration;
128 int missing_field = 0;
129 unsigned long i;
130
131 /*
132 * Strictly speaking, each enumerator must map to a field of the
133 * variant. However, we are even stricter here by requiring that each
134 * variant choice map to an enumerator too. We then validate that the
135 * number of enumerators equals the number of variant choices.
136 */
137 if (variant->declaration->untagged_variant->fields->len != enum_get_nr_enumerators(enum_declaration))
138 return -EPERM;
139
140 for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
141 struct declaration_field *field_declaration =
142 &g_array_index(variant->declaration->untagged_variant->fields,
143 struct declaration_field, i);
144 if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
145 missing_field = 1;
146 break;
147 }
148 }
149 if (missing_field)
150 return -EPERM;
151
152 /*
153 * Check the enumeration: it must map each value to one and only one
154 * enumerator tag.
155 * TODO: we should also check that each range map to one and only one
156 * tag. For the moment, we will simply check this dynamically in
157 * variant_declaration_get_current_field().
158 */
159 return 0;
160 }
161
162
163
164 static
165 struct definition *
166 _variant_definition_new(struct declaration *declaration,
167 struct definition_scope *parent_scope,
168 GQuark field_name, int index,
169 const char *root_name)
170 {
171 struct declaration_variant *variant_declaration =
172 container_of(declaration, struct declaration_variant, p);
173 struct definition_variant *variant;
174 unsigned long i;
175 int ret;
176
177 variant = g_new(struct definition_variant, 1);
178 declaration_ref(&variant_declaration->p);
179 variant->p.declaration = declaration;
180 variant->declaration = variant_declaration;
181 variant->p.ref = 1;
182 /*
183 * Use INT_MAX order to ensure that all fields of the parent
184 * scope are seen as being prior to this scope.
185 */
186 variant->p.index = root_name ? INT_MAX : index;
187 variant->p.name = field_name;
188 variant->p.path = new_definition_path(parent_scope, field_name, root_name);
189 variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
190
191 ret = register_field_definition(field_name, &variant->p,
192 parent_scope);
193 assert(!ret);
194
195 variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
196 variant_declaration->tag_name,
197 parent_scope);
198
199 if (!variant->enum_tag
200 || check_enum_tag(variant, variant->enum_tag) < 0)
201 goto error;
202 definition_ref(variant->enum_tag);
203 variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
204 g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
205 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
206 struct declaration_field *declaration_field =
207 &g_array_index(variant_declaration->untagged_variant->fields,
208 struct declaration_field, i);
209 struct definition **field =
210 (struct definition **) &g_ptr_array_index(variant->fields, i);
211
212 /*
213 * All child definition are at index 0, because they are
214 * various choices of the same field.
215 */
216 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
217 variant->p.scope,
218 declaration_field->name, 0, NULL);
219 if (!*field)
220 goto error;
221 }
222 variant->current_field = NULL;
223 return &variant->p;
224 error:
225 free_definition_scope(variant->p.scope);
226 declaration_unref(&variant_declaration->p);
227 g_free(variant);
228 return NULL;
229 }
230
231 static
232 void _variant_definition_free(struct definition *definition)
233 {
234 struct definition_variant *variant =
235 container_of(definition, struct definition_variant, p);
236 unsigned long i;
237
238 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
239 for (i = 0; i < variant->fields->len; i++) {
240 struct definition *field = g_ptr_array_index(variant->fields, i);
241 definition_unref(field);
242 }
243 definition_unref(variant->enum_tag);
244 free_definition_scope(variant->p.scope);
245 declaration_unref(variant->p.declaration);
246 g_free(variant);
247 }
248
249 void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
250 const char *field_name,
251 struct declaration *field_declaration)
252 {
253 struct declaration_field *field;
254 unsigned long index;
255
256 g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
257 index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
258 field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
259 field->name = g_quark_from_string(field_name);
260 declaration_ref(field_declaration);
261 field->declaration = field_declaration;
262 /* Keep index in hash rather than pointer, because array can relocate */
263 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
264 (gpointer) (unsigned long) field->name,
265 (gpointer) index);
266 /*
267 * Alignment of variant is based on the alignment of its currently
268 * selected choice, so we leave variant alignment as-is (statically
269 * speaking).
270 */
271 }
272
273 struct declaration_field *
274 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
275 {
276 unsigned long index;
277
278 index = (unsigned long) g_hash_table_lookup(untagged_variant_declaration->fields_by_tag,
279 (gconstpointer) (unsigned long) tag);
280 return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
281 }
282
283 /*
284 * field returned only valid as long as the field structure is not appended to.
285 */
286 struct definition *variant_get_current_field(struct definition_variant *variant)
287 {
288 struct definition_enum *_enum =
289 container_of(variant->enum_tag, struct definition_enum, p);
290 struct declaration_variant *variant_declaration = variant->declaration;
291 unsigned long index;
292 GArray *tag_array;
293 GQuark tag;
294
295 tag_array = _enum->value;
296 /*
297 * The 1 to 1 mapping from enumeration to value should have been already
298 * checked. (see TODO above)
299 */
300 assert(tag_array->len == 1);
301 tag = g_array_index(tag_array, GQuark, 0);
302 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
303 (gconstpointer) (unsigned long) tag);
304 variant->current_field = g_ptr_array_index(variant->fields, index);
305 return variant->current_field;
306 }
This page took 0.035242 seconds and 4 git commands to generate.