Commit | Line | Data |
---|---|---|
c054553d MD |
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 | ||
22 | ||
23 | static | |
e19c3d69 MD |
24 | struct declaration *_variant_declaration_new(struct type *type, |
25 | struct declaration_scope *parent_scope); | |
c054553d | 26 | static |
e19c3d69 | 27 | void _variant_declaration_free(struct declaration *declaration); |
c054553d MD |
28 | |
29 | void variant_copy(struct stream_pos *dest, const struct format *fdest, | |
30 | struct stream_pos *src, const struct format *fsrc, | |
e19c3d69 | 31 | struct declaration *declaration) |
c054553d | 32 | { |
e19c3d69 MD |
33 | struct declaration_variant *variant = |
34 | container_of(declaration, struct declaration_variant, p); | |
35 | struct type_variant *variant_type = variant->type; | |
c054553d | 36 | struct field *field; |
e19c3d69 | 37 | struct type *field_type; |
c054553d MD |
38 | unsigned long i; |
39 | ||
e19c3d69 MD |
40 | fsrc->variant_begin(src, variant_type); |
41 | fdest->variant_begin(dest, variant_type); | |
c054553d | 42 | |
e19c3d69 MD |
43 | field = variant_get_current_field(variant); |
44 | field_type = field->type->p.type; | |
45 | field_type->copy(dest, fdest, src, fsrc, &field->type->p); | |
c054553d | 46 | |
e19c3d69 MD |
47 | fsrc->variant_end(src, variant_type); |
48 | fdest->variant_end(dest, variant_type); | |
c054553d MD |
49 | } |
50 | ||
51 | static | |
e19c3d69 | 52 | void _variant_type_free(struct type *type) |
c054553d | 53 | { |
e19c3d69 MD |
54 | struct type_variant *variant_type = |
55 | container_of(type, struct type_variant, p); | |
c054553d MD |
56 | unsigned long i; |
57 | ||
e19c3d69 | 58 | g_hash_table_destroy(variant_type->fields_by_tag); |
c054553d | 59 | |
e19c3d69 MD |
60 | for (i = 0; i < variant_type->fields->len; i++) { |
61 | struct field *type_field = | |
62 | &g_array_index(variant_type->fields, | |
63 | struct type_field, i); | |
64 | type_unref(field->type); | |
c054553d | 65 | } |
e19c3d69 MD |
66 | g_array_free(variant_type->fields, true); |
67 | g_free(variant_type); | |
c054553d MD |
68 | } |
69 | ||
e19c3d69 | 70 | struct type_variant *variant_type_new(const char *name) |
c054553d | 71 | { |
e19c3d69 MD |
72 | struct type_variant *variant_type; |
73 | struct type *type; | |
c054553d MD |
74 | int ret; |
75 | ||
e19c3d69 MD |
76 | variant_type = g_new(struct type_variant, 1); |
77 | type = &variant_type->p; | |
78 | variant_type->fields_by_tag = g_hash_table_new(g_direct_hash, | |
79 | g_direct_equal); | |
80 | variant_type->fields = g_array_sized_new(FALSE, TRUE, | |
81 | sizeof(struct type_field), | |
82 | DEFAULT_NR_STRUCT_FIELDS); | |
83 | type->name = g_quark_from_string(name); | |
84 | type->alignment = 1; | |
85 | type->copy = variant_copy; | |
86 | type->type_free = _variant_type_free; | |
87 | type->declaration_new = _variant_declaration_new; | |
88 | type->declaration_free = _variant_declaration_free; | |
89 | type->ref = 1; | |
90 | ||
91 | if (type->name) { | |
92 | ret = register_type(type); | |
c054553d MD |
93 | if (ret) |
94 | goto error_register; | |
95 | } | |
e19c3d69 | 96 | return variant_type; |
c054553d MD |
97 | |
98 | error_register: | |
e19c3d69 MD |
99 | g_hash_table_destroy(variant_type->fields_by_tag); |
100 | g_array_free(variant_type->fields, true); | |
101 | g_free(variant_type); | |
c054553d MD |
102 | return NULL; |
103 | } | |
104 | ||
105 | static | |
e19c3d69 MD |
106 | struct declaration * |
107 | _variant_declaration_new(struct type *type, | |
108 | struct declaration_scope *parent_scope) | |
c054553d | 109 | { |
e19c3d69 MD |
110 | struct type_variant *variant_type = |
111 | container_of(type, struct type_variant, p); | |
112 | struct declaration_variant *variant; | |
c054553d | 113 | |
e19c3d69 MD |
114 | variant = g_new(struct declaration_variant, 1); |
115 | type_ref(&variant_type->p); | |
116 | variant->p.type = variant_type; | |
c054553d MD |
117 | variant->p.ref = 1; |
118 | variant->scope = new_declaration_scope(parent_scope); | |
119 | variant->fields = g_array_sized_new(FALSE, TRUE, | |
120 | sizeof(struct field), | |
121 | DEFAULT_NR_STRUCT_FIELDS); | |
122 | variant->current_field = NULL; | |
123 | return &variant->p; | |
124 | } | |
125 | ||
126 | static | |
e19c3d69 | 127 | void variant_declaration_free(struct declaration *declaration) |
c054553d | 128 | { |
e19c3d69 MD |
129 | struct declaration_variant *variant = |
130 | container_of(declaration, struct declaration_variant, p); | |
c054553d MD |
131 | unsigned long i; |
132 | ||
133 | for (i = 0; i < variant->fields->len; i++) { | |
134 | struct field *field = &g_array_index(variant->fields, | |
135 | struct field, i); | |
e19c3d69 | 136 | declaration_unref(field->declaration); |
c054553d MD |
137 | } |
138 | free_declaration_scope(variant->scope); | |
e19c3d69 | 139 | type_unref(variant->p.type); |
c054553d MD |
140 | g_free(variant); |
141 | } | |
142 | ||
e19c3d69 MD |
143 | void variant_type_add_field(struct type_variant *variant_type, |
144 | const char *tag_name, | |
145 | struct type *tag_type) | |
c054553d | 146 | { |
e19c3d69 | 147 | struct type_field *field; |
c054553d MD |
148 | unsigned long index; |
149 | ||
e19c3d69 MD |
150 | g_array_set_size(variant_type->fields, variant_type->fields->len + 1); |
151 | index = variant_type->fields->len - 1; /* last field (new) */ | |
152 | field = &g_array_index(variant_type->fields, struct type_field, index); | |
c054553d | 153 | field->name = g_quark_from_string(tag_name); |
e19c3d69 MD |
154 | type_ref(tag_type); |
155 | field->type = tag_type; | |
c054553d | 156 | /* Keep index in hash rather than pointer, because array can relocate */ |
e19c3d69 | 157 | g_hash_table_insert(variant_type->fields_by_name, |
c054553d MD |
158 | (gpointer) (unsigned long) field->name, |
159 | (gpointer) index); | |
160 | /* | |
161 | * Alignment of variant is based on the alignment of its currently | |
162 | * selected choice, so we leave variant alignment as-is (statically | |
163 | * speaking). | |
164 | */ | |
165 | } | |
166 | ||
e19c3d69 MD |
167 | struct type_field * |
168 | struct_type_get_field_from_tag(struct type_variant *variant_type, GQuark tag) | |
c054553d MD |
169 | { |
170 | unsigned long index; | |
171 | ||
e19c3d69 | 172 | index = (unsigned long) g_hash_table_lookup(variant_type->fields_by_tag, |
c054553d | 173 | (gconstpointer) (unsigned long) tag); |
e19c3d69 | 174 | return &g_array_index(variant_type->fields, struct type_field, index); |
c054553d MD |
175 | } |
176 | ||
177 | /* | |
178 | * tag_instance is assumed to be an enumeration. | |
179 | */ | |
e19c3d69 MD |
180 | int variant_declaration_set_tag(struct declaration_variant *variant, |
181 | struct declaration *enum_tag) | |
c054553d | 182 | { |
e19c3d69 MD |
183 | struct declaration_enum *_enum = |
184 | container_of(variant->enum_tag, struct declaration_enum, p); | |
185 | struct type_enum *enum_type = _enum->type; | |
c054553d MD |
186 | int missing_field = 0; |
187 | unsigned long i; | |
188 | ||
189 | /* | |
190 | * Strictly speaking, each enumerator must map to a field of the | |
191 | * variant. However, we are even stricter here by requiring that each | |
192 | * variant choice map to an enumerator too. We then validate that the | |
193 | * number of enumerators equals the number of variant choices. | |
194 | */ | |
e19c3d69 | 195 | if (variant->type->fields->len != enum_get_nr_enumerators(enum_type)) |
c054553d MD |
196 | return -EPERM; |
197 | ||
e19c3d69 MD |
198 | for (i = 0; i < variant->type->fields->len; i++) { |
199 | struct type_field *field_type = | |
200 | &g_array_index(variant->type->fields, | |
201 | struct type_field, i); | |
202 | if (!enum_quark_to_range_set(enum_type, field_type->name)) { | |
c054553d MD |
203 | missing_field = 1; |
204 | break; | |
205 | } | |
206 | } | |
207 | if (missing_field) | |
208 | return -EPERM; | |
209 | ||
210 | /* | |
211 | * Check the enumeration: it must map each value to one and only one | |
212 | * enumerator tag. | |
213 | * TODO: we should also check that each range map to one and only one | |
214 | * tag. For the moment, we will simply check this dynamically in | |
215 | * variant_type_get_current_field(). | |
216 | */ | |
217 | ||
218 | /* Set the enum tag field */ | |
e19c3d69 | 219 | variant->enum_tag = enum_tag; |
c054553d MD |
220 | return 0; |
221 | } | |
222 | ||
223 | /* | |
224 | * field returned only valid as long as the field structure is not appended to. | |
225 | */ | |
e19c3d69 | 226 | struct field *variant_get_current_field(struct declaration_variant *variant) |
c054553d | 227 | { |
e19c3d69 MD |
228 | struct declaration_enum *_enum = |
229 | container_of(variant->enum_tag, struct declaration_enum, p); | |
230 | struct variant_type *variant_type = variant->type; | |
c054553d MD |
231 | unsigned long index; |
232 | GArray *tag_array; | |
233 | GQuark tag; | |
234 | ||
235 | tag_array = _enum->value; | |
236 | /* | |
237 | * The 1 to 1 mapping from enumeration to value should have been already | |
238 | * checked. (see TODO above) | |
239 | */ | |
240 | assert(tag_array->len == 1); | |
241 | tag = g_array_index(tag_array, GQuark, 0); | |
e19c3d69 | 242 | index = (unsigned long) g_hash_table_lookup(variant_type->fields_by_tag, |
c054553d | 243 | (gconstpointer) (unsigned long) tag); |
e19c3d69 | 244 | variant->current_field = &g_array_index(variant->fields, struct field, index); |
96354dac | 245 | return variant->current_field; |
c054553d | 246 | } |