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