Commit | Line | Data |
---|---|---|
c054553d | 1 | /* |
ab4cf058 | 2 | * variant.c |
c054553d MD |
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> | |
6ee5115e | 21 | #include <errno.h> |
c054553d MD |
22 | |
23 | static | |
f6625916 | 24 | struct definition *_variant_definition_new(struct declaration *declaration, |
05c749e5 MD |
25 | struct definition_scope *parent_scope, |
26 | GQuark field_name, int index); | |
c054553d | 27 | static |
e1151715 | 28 | void _variant_definition_free(struct definition *definition); |
c054553d MD |
29 | |
30 | void variant_copy(struct stream_pos *dest, const struct format *fdest, | |
31 | struct stream_pos *src, const struct format *fsrc, | |
e1151715 | 32 | struct definition *definition) |
c054553d | 33 | { |
e1151715 MD |
34 | struct definition_variant *variant = |
35 | container_of(definition, struct definition_variant, p); | |
f6625916 | 36 | struct declaration_variant *variant_declaration = variant->declaration; |
c054553d | 37 | struct field *field; |
f6625916 | 38 | struct declaration *field_declaration; |
c054553d | 39 | |
f6625916 MD |
40 | fsrc->variant_begin(src, variant_declaration); |
41 | fdest->variant_begin(dest, variant_declaration); | |
c054553d | 42 | |
e19c3d69 | 43 | field = variant_get_current_field(variant); |
f6625916 MD |
44 | field_declaration = field->definition->declaration; |
45 | field_declaration->copy(dest, fdest, src, fsrc, field->definition); | |
c054553d | 46 | |
f6625916 MD |
47 | fsrc->variant_end(src, variant_declaration); |
48 | fdest->variant_end(dest, variant_declaration); | |
c054553d MD |
49 | } |
50 | ||
51 | static | |
d60cb676 | 52 | void _untagged_variant_declaration_free(struct declaration *declaration) |
c054553d | 53 | { |
d60cb676 MD |
54 | struct declaration_untagged_variant *untagged_variant_declaration = |
55 | container_of(declaration, struct declaration_untagged_variant, p); | |
c054553d MD |
56 | unsigned long i; |
57 | ||
d60cb676 MD |
58 | free_declaration_scope(untagged_variant_declaration->scope); |
59 | g_hash_table_destroy(untagged_variant_declaration->fields_by_tag); | |
c054553d | 60 | |
d60cb676 | 61 | for (i = 0; i < untagged_variant_declaration->fields->len; i++) { |
f6625916 | 62 | struct declaration_field *declaration_field = |
d60cb676 | 63 | &g_array_index(untagged_variant_declaration->fields, |
f6625916 MD |
64 | struct declaration_field, i); |
65 | declaration_unref(declaration_field->declaration); | |
c054553d | 66 | } |
d60cb676 MD |
67 | g_array_free(untagged_variant_declaration->fields, true); |
68 | g_free(untagged_variant_declaration); | |
c054553d MD |
69 | } |
70 | ||
ab4cf058 | 71 | struct declaration_untagged_variant *untagged_variant_declaration_new( |
f6625916 | 72 | struct declaration_scope *parent_scope) |
c054553d | 73 | { |
d60cb676 | 74 | struct declaration_untagged_variant *untagged_variant_declaration; |
f6625916 | 75 | struct declaration *declaration; |
c054553d | 76 | |
d60cb676 MD |
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, | |
e19c3d69 | 80 | g_direct_equal); |
d60cb676 | 81 | untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE, |
f6625916 | 82 | sizeof(struct declaration_field), |
e19c3d69 | 83 | DEFAULT_NR_STRUCT_FIELDS); |
d60cb676 MD |
84 | untagged_variant_declaration->scope = new_declaration_scope(parent_scope); |
85 | declaration->id = CTF_TYPE_UNTAGGED_VARIANT; | |
f6625916 | 86 | declaration->alignment = 1; |
d60cb676 MD |
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; | |
f6625916 MD |
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; | |
c054553d MD |
124 | } |
125 | ||
d60cb676 MD |
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 | ||
c054553d | 173 | static |
e1151715 | 174 | struct definition * |
f6625916 | 175 | _variant_definition_new(struct declaration *declaration, |
05c749e5 MD |
176 | struct definition_scope *parent_scope, |
177 | GQuark field_name, int index) | |
c054553d | 178 | { |
f6625916 MD |
179 | struct declaration_variant *variant_declaration = |
180 | container_of(declaration, struct declaration_variant, p); | |
e1151715 | 181 | struct definition_variant *variant; |
ac88af75 | 182 | unsigned long i; |
c054553d | 183 | |
e1151715 | 184 | variant = g_new(struct definition_variant, 1); |
f6625916 MD |
185 | declaration_ref(&variant_declaration->p); |
186 | variant->p.declaration = declaration; | |
187 | variant->declaration = variant_declaration; | |
c054553d | 188 | variant->p.ref = 1; |
05c749e5 MD |
189 | variant->p.index = index; |
190 | variant->scope = new_definition_scope(parent_scope, field_name); | |
d60cb676 MD |
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); | |
c054553d MD |
199 | variant->fields = g_array_sized_new(FALSE, TRUE, |
200 | sizeof(struct field), | |
d60cb676 MD |
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++) { | |
f6625916 | 204 | struct declaration_field *declaration_field = |
d60cb676 | 205 | &g_array_index(variant_declaration->untagged_variant->fields, |
f6625916 | 206 | struct declaration_field, i); |
ac88af75 MD |
207 | struct field *field = &g_array_index(variant->fields, |
208 | struct field, i); | |
209 | ||
f6625916 | 210 | field->name = declaration_field->name; |
05c749e5 MD |
211 | /* |
212 | * All child definition are at index 0, because they are | |
213 | * various choices of the same field. | |
214 | */ | |
e1151715 | 215 | field->definition = |
f6625916 | 216 | declaration_field->declaration->definition_new(declaration_field->declaration, |
05c749e5 MD |
217 | variant->scope, |
218 | field->name, 0); | |
ac88af75 | 219 | } |
c054553d MD |
220 | variant->current_field = NULL; |
221 | return &variant->p; | |
d60cb676 MD |
222 | error: |
223 | free_definition_scope(variant->scope); | |
224 | declaration_unref(&variant_declaration->p); | |
225 | g_free(variant); | |
226 | return NULL; | |
c054553d MD |
227 | } |
228 | ||
229 | static | |
e1151715 | 230 | void _variant_definition_free(struct definition *definition) |
c054553d | 231 | { |
e1151715 MD |
232 | struct definition_variant *variant = |
233 | container_of(definition, struct definition_variant, p); | |
c054553d MD |
234 | unsigned long i; |
235 | ||
d60cb676 | 236 | assert(variant->fields->len == variant->declaration->untagged_variant->fields->len); |
c054553d MD |
237 | for (i = 0; i < variant->fields->len; i++) { |
238 | struct field *field = &g_array_index(variant->fields, | |
239 | struct field, i); | |
e1151715 | 240 | definition_unref(field->definition); |
c054553d | 241 | } |
d60cb676 | 242 | definition_unref(variant->enum_tag); |
e1151715 | 243 | free_definition_scope(variant->scope); |
f6625916 | 244 | declaration_unref(variant->p.declaration); |
c054553d MD |
245 | g_free(variant); |
246 | } | |
247 | ||
d60cb676 MD |
248 | void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration, |
249 | const char *field_name, | |
250 | struct declaration *field_declaration) | |
c054553d | 251 | { |
f6625916 | 252 | struct declaration_field *field; |
c054553d MD |
253 | unsigned long index; |
254 | ||
d60cb676 MD |
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; | |
c054553d | 261 | /* Keep index in hash rather than pointer, because array can relocate */ |
d60cb676 | 262 | g_hash_table_insert(untagged_variant_declaration->fields_by_tag, |
c054553d MD |
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 | ||
f6625916 | 272 | struct declaration_field * |
d60cb676 | 273 | untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag) |
c054553d MD |
274 | { |
275 | unsigned long index; | |
276 | ||
d60cb676 | 277 | index = (unsigned long) g_hash_table_lookup(untagged_variant_declaration->fields_by_tag, |
c054553d | 278 | (gconstpointer) (unsigned long) tag); |
d60cb676 | 279 | return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index); |
c054553d MD |
280 | } |
281 | ||
282 | /* | |
283 | * field returned only valid as long as the field structure is not appended to. | |
284 | */ | |
e1151715 | 285 | struct field *variant_get_current_field(struct definition_variant *variant) |
c054553d | 286 | { |
e1151715 MD |
287 | struct definition_enum *_enum = |
288 | container_of(variant->enum_tag, struct definition_enum, p); | |
f6625916 | 289 | struct declaration_variant *variant_declaration = variant->declaration; |
c054553d MD |
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); | |
d60cb676 | 301 | index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag, |
c054553d | 302 | (gconstpointer) (unsigned long) tag); |
e19c3d69 | 303 | variant->current_field = &g_array_index(variant->fields, struct field, index); |
96354dac | 304 | return variant->current_field; |
c054553d | 305 | } |