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