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