Remove unneeded declaration "name", work in progress for gen io struct
[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(
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->alignment = 1;
87 declaration->copy = NULL;
88 declaration->declaration_free = _untagged_variant_declaration_free;
89 declaration->definition_new = NULL;
90 declaration->definition_free = NULL;
91 declaration->ref = 1;
92 return untagged_variant_declaration;
93 }
94
95 static
96 void _variant_declaration_free(struct declaration *declaration)
97 {
98 struct declaration_variant *variant_declaration =
99 container_of(declaration, struct declaration_variant, p);
100
101 _untagged_variant_declaration_free(&variant_declaration->untagged_variant->p);
102 g_array_free(variant_declaration->tag_name, TRUE);
103 }
104
105 struct declaration_variant *
106 variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
107 {
108 struct declaration_variant *variant_declaration;
109 struct declaration *declaration;
110
111 variant_declaration = g_new(struct declaration_variant, 1);
112 declaration = &variant_declaration->p;
113 variant_declaration->untagged_variant = untagged_variant;
114 variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
115 append_scope_path(tag, variant_declaration->tag_name);
116 declaration->id = CTF_TYPE_VARIANT;
117 declaration->alignment = 1;
118 declaration->copy = variant_copy;
119 declaration->declaration_free = _variant_declaration_free;
120 declaration->definition_new = _variant_definition_new;
121 declaration->definition_free = _variant_definition_free;
122 declaration->ref = 1;
123 return variant_declaration;
124 }
125
126 /*
127 * tag_instance is assumed to be an enumeration.
128 * Returns 0 if OK, < 0 if error.
129 */
130 static
131 int check_enum_tag(struct definition_variant *variant,
132 struct definition *enum_tag)
133 {
134 struct definition_enum *_enum =
135 container_of(enum_tag, struct definition_enum, p);
136 struct declaration_enum *enum_declaration = _enum->declaration;
137 int missing_field = 0;
138 unsigned long i;
139
140 /*
141 * Strictly speaking, each enumerator must map to a field of the
142 * variant. However, we are even stricter here by requiring that each
143 * variant choice map to an enumerator too. We then validate that the
144 * number of enumerators equals the number of variant choices.
145 */
146 if (variant->declaration->untagged_variant->fields->len != enum_get_nr_enumerators(enum_declaration))
147 return -EPERM;
148
149 for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
150 struct declaration_field *field_declaration =
151 &g_array_index(variant->declaration->untagged_variant->fields,
152 struct declaration_field, i);
153 if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
154 missing_field = 1;
155 break;
156 }
157 }
158 if (missing_field)
159 return -EPERM;
160
161 /*
162 * Check the enumeration: it must map each value to one and only one
163 * enumerator tag.
164 * TODO: we should also check that each range map to one and only one
165 * tag. For the moment, we will simply check this dynamically in
166 * variant_declaration_get_current_field().
167 */
168 return 0;
169 }
170
171
172
173 static
174 struct definition *
175 _variant_definition_new(struct declaration *declaration,
176 struct definition_scope *parent_scope,
177 GQuark field_name, int index)
178 {
179 struct declaration_variant *variant_declaration =
180 container_of(declaration, struct declaration_variant, p);
181 struct definition_variant *variant;
182 unsigned long i;
183
184 variant = g_new(struct definition_variant, 1);
185 declaration_ref(&variant_declaration->p);
186 variant->p.declaration = declaration;
187 variant->declaration = variant_declaration;
188 variant->p.ref = 1;
189 variant->p.index = index;
190 variant->scope = new_definition_scope(parent_scope, field_name);
191 variant->enum_tag = lookup_definition(variant->scope->scope_path,
192 variant_declaration->tag_name,
193 parent_scope);
194
195 if (!variant->enum_tag
196 || check_enum_tag(variant, variant->enum_tag) < 0)
197 goto error;
198 definition_ref(variant->enum_tag);
199 variant->fields = g_array_sized_new(FALSE, TRUE,
200 sizeof(struct field),
201 variant_declaration->untagged_variant->fields->len);
202 g_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
203 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
204 struct declaration_field *declaration_field =
205 &g_array_index(variant_declaration->untagged_variant->fields,
206 struct declaration_field, i);
207 struct field *field = &g_array_index(variant->fields,
208 struct field, i);
209
210 field->name = declaration_field->name;
211 /*
212 * All child definition are at index 0, because they are
213 * various choices of the same field.
214 */
215 field->definition =
216 declaration_field->declaration->definition_new(declaration_field->declaration,
217 variant->scope,
218 field->name, 0);
219 }
220 variant->current_field = NULL;
221 return &variant->p;
222 error:
223 free_definition_scope(variant->scope);
224 declaration_unref(&variant_declaration->p);
225 g_free(variant);
226 return NULL;
227 }
228
229 static
230 void _variant_definition_free(struct definition *definition)
231 {
232 struct definition_variant *variant =
233 container_of(definition, struct definition_variant, p);
234 unsigned long i;
235
236 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
237 for (i = 0; i < variant->fields->len; i++) {
238 struct field *field = &g_array_index(variant->fields,
239 struct field, i);
240 definition_unref(field->definition);
241 }
242 definition_unref(variant->enum_tag);
243 free_definition_scope(variant->scope);
244 declaration_unref(variant->p.declaration);
245 g_free(variant);
246 }
247
248 void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
249 const char *field_name,
250 struct declaration *field_declaration)
251 {
252 struct declaration_field *field;
253 unsigned long index;
254
255 g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
256 index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
257 field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
258 field->name = g_quark_from_string(field_name);
259 declaration_ref(field_declaration);
260 field->declaration = field_declaration;
261 /* Keep index in hash rather than pointer, because array can relocate */
262 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
263 (gpointer) (unsigned long) field->name,
264 (gpointer) index);
265 /*
266 * Alignment of variant is based on the alignment of its currently
267 * selected choice, so we leave variant alignment as-is (statically
268 * speaking).
269 */
270 }
271
272 struct declaration_field *
273 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
274 {
275 unsigned long index;
276
277 index = (unsigned long) g_hash_table_lookup(untagged_variant_declaration->fields_by_tag,
278 (gconstpointer) (unsigned long) tag);
279 return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
280 }
281
282 /*
283 * field returned only valid as long as the field structure is not appended to.
284 */
285 struct field *variant_get_current_field(struct definition_variant *variant)
286 {
287 struct definition_enum *_enum =
288 container_of(variant->enum_tag, struct definition_enum, p);
289 struct declaration_variant *variant_declaration = variant->declaration;
290 unsigned long index;
291 GArray *tag_array;
292 GQuark tag;
293
294 tag_array = _enum->value;
295 /*
296 * The 1 to 1 mapping from enumeration to value should have been already
297 * checked. (see TODO above)
298 */
299 assert(tag_array->len == 1);
300 tag = g_array_index(tag_array, GQuark, 0);
301 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
302 (gconstpointer) (unsigned long) tag);
303 variant->current_field = &g_array_index(variant->fields, struct field, index);
304 return variant->current_field;
305 }
This page took 0.035992 seconds and 5 git commands to generate.