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