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