Create "untagged" variant 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 fdest->variant_begin(dest, variant_declaration);
42
43 field = variant_get_current_field(variant);
44 field_declaration = field->definition->declaration;
45 field_declaration->copy(dest, fdest, src, fsrc, field->definition);
46
47 fsrc->variant_end(src, variant_declaration);
48 fdest->variant_end(dest, variant_declaration);
49 }
50
51 static
52 void _untagged_variant_declaration_free(struct declaration *declaration)
53 {
54 struct declaration_untagged_variant *untagged_variant_declaration =
55 container_of(declaration, struct declaration_untagged_variant, p);
56 unsigned long i;
57
58 free_declaration_scope(untagged_variant_declaration->scope);
59 g_hash_table_destroy(untagged_variant_declaration->fields_by_tag);
60
61 for (i = 0; i < untagged_variant_declaration->fields->len; i++) {
62 struct declaration_field *declaration_field =
63 &g_array_index(untagged_variant_declaration->fields,
64 struct declaration_field, i);
65 declaration_unref(declaration_field->declaration);
66 }
67 g_array_free(untagged_variant_declaration->fields, true);
68 g_free(untagged_variant_declaration);
69 }
70
71 struct declaration_untagged_variant *untagged_variant_declaration_new(const char *name,
72 struct declaration_scope *parent_scope)
73 {
74 struct declaration_untagged_variant *untagged_variant_declaration;
75 struct declaration *declaration;
76
77 untagged_variant_declaration = g_new(struct declaration_untagged_variant, 1);
78 declaration = &untagged_variant_declaration->p;
79 untagged_variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
80 g_direct_equal);
81 untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
82 sizeof(struct declaration_field),
83 DEFAULT_NR_STRUCT_FIELDS);
84 untagged_variant_declaration->scope = new_declaration_scope(parent_scope);
85 declaration->id = CTF_TYPE_UNTAGGED_VARIANT;
86 declaration->name = g_quark_from_string(name);
87 declaration->alignment = 1;
88 declaration->copy = NULL;
89 declaration->declaration_free = _untagged_variant_declaration_free;
90 declaration->definition_new = NULL;
91 declaration->definition_free = NULL;
92 declaration->ref = 1;
93 return untagged_variant_declaration;
94 }
95
96 static
97 void _variant_declaration_free(struct declaration *declaration)
98 {
99 struct declaration_variant *variant_declaration =
100 container_of(declaration, struct declaration_variant, p);
101
102 _untagged_variant_declaration_free(&variant_declaration->untagged_variant->p);
103 g_array_free(variant_declaration->tag_name, TRUE);
104 }
105
106 struct declaration_variant *
107 variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
108 {
109 struct declaration_variant *variant_declaration;
110 struct declaration *declaration;
111
112 variant_declaration = g_new(struct declaration_variant, 1);
113 declaration = &variant_declaration->p;
114 variant_declaration->untagged_variant = untagged_variant;
115 variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
116 append_scope_path(tag, variant_declaration->tag_name);
117 declaration->id = CTF_TYPE_VARIANT;
118 declaration->alignment = 1;
119 declaration->copy = variant_copy;
120 declaration->declaration_free = _variant_declaration_free;
121 declaration->definition_new = _variant_definition_new;
122 declaration->definition_free = _variant_definition_free;
123 declaration->ref = 1;
124 return variant_declaration;
125 }
126
127 /*
128 * tag_instance is assumed to be an enumeration.
129 * Returns 0 if OK, < 0 if error.
130 */
131 static
132 int check_enum_tag(struct definition_variant *variant,
133 struct definition *enum_tag)
134 {
135 struct definition_enum *_enum =
136 container_of(enum_tag, struct definition_enum, p);
137 struct declaration_enum *enum_declaration = _enum->declaration;
138 int missing_field = 0;
139 unsigned long i;
140
141 /*
142 * Strictly speaking, each enumerator must map to a field of the
143 * variant. However, we are even stricter here by requiring that each
144 * variant choice map to an enumerator too. We then validate that the
145 * number of enumerators equals the number of variant choices.
146 */
147 if (variant->declaration->untagged_variant->fields->len != enum_get_nr_enumerators(enum_declaration))
148 return -EPERM;
149
150 for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
151 struct declaration_field *field_declaration =
152 &g_array_index(variant->declaration->untagged_variant->fields,
153 struct declaration_field, i);
154 if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
155 missing_field = 1;
156 break;
157 }
158 }
159 if (missing_field)
160 return -EPERM;
161
162 /*
163 * Check the enumeration: it must map each value to one and only one
164 * enumerator tag.
165 * TODO: we should also check that each range map to one and only one
166 * tag. For the moment, we will simply check this dynamically in
167 * variant_declaration_get_current_field().
168 */
169 return 0;
170 }
171
172
173
174 static
175 struct definition *
176 _variant_definition_new(struct declaration *declaration,
177 struct definition_scope *parent_scope,
178 GQuark field_name, int index)
179 {
180 struct declaration_variant *variant_declaration =
181 container_of(declaration, struct declaration_variant, p);
182 struct definition_variant *variant;
183 unsigned long i;
184
185 variant = g_new(struct definition_variant, 1);
186 declaration_ref(&variant_declaration->p);
187 variant->p.declaration = declaration;
188 variant->declaration = variant_declaration;
189 variant->p.ref = 1;
190 variant->p.index = index;
191 variant->scope = new_definition_scope(parent_scope, field_name);
192 variant->enum_tag = lookup_definition(variant->scope->scope_path,
193 variant_declaration->tag_name,
194 parent_scope);
195
196 if (!variant->enum_tag
197 || check_enum_tag(variant, variant->enum_tag) < 0)
198 goto error;
199 definition_ref(variant->enum_tag);
200 variant->fields = g_array_sized_new(FALSE, TRUE,
201 sizeof(struct field),
202 variant_declaration->untagged_variant->fields->len);
203 g_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
204 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
205 struct declaration_field *declaration_field =
206 &g_array_index(variant_declaration->untagged_variant->fields,
207 struct declaration_field, i);
208 struct field *field = &g_array_index(variant->fields,
209 struct field, i);
210
211 field->name = declaration_field->name;
212 /*
213 * All child definition are at index 0, because they are
214 * various choices of the same field.
215 */
216 field->definition =
217 declaration_field->declaration->definition_new(declaration_field->declaration,
218 variant->scope,
219 field->name, 0);
220 }
221 variant->current_field = NULL;
222 return &variant->p;
223 error:
224 free_definition_scope(variant->scope);
225 declaration_unref(&variant_declaration->p);
226 g_free(variant);
227 return NULL;
228 }
229
230 static
231 void _variant_definition_free(struct definition *definition)
232 {
233 struct definition_variant *variant =
234 container_of(definition, struct definition_variant, p);
235 unsigned long i;
236
237 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
238 for (i = 0; i < variant->fields->len; i++) {
239 struct field *field = &g_array_index(variant->fields,
240 struct field, i);
241 definition_unref(field->definition);
242 }
243 definition_unref(variant->enum_tag);
244 free_definition_scope(variant->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 field *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_array_index(variant->fields, struct field, index);
305 return variant->current_field;
306 }
This page took 0.034437 seconds and 4 git commands to generate.