Rename "type" to "declaration"
[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 definition *_variant_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope);
26 static
27 void _variant_definition_free(struct definition *definition);
28
29 void variant_copy(struct stream_pos *dest, const struct format *fdest,
30 struct stream_pos *src, const struct format *fsrc,
31 struct definition *definition)
32 {
33 struct definition_variant *variant =
34 container_of(definition, struct definition_variant, p);
35 struct declaration_variant *variant_declaration = variant->declaration;
36 struct field *field;
37 struct declaration *field_declaration;
38
39 fsrc->variant_begin(src, variant_declaration);
40 fdest->variant_begin(dest, variant_declaration);
41
42 field = variant_get_current_field(variant);
43 field_declaration = field->definition->declaration;
44 field_declaration->copy(dest, fdest, src, fsrc, field->definition);
45
46 fsrc->variant_end(src, variant_declaration);
47 fdest->variant_end(dest, variant_declaration);
48 }
49
50 static
51 void _variant_declaration_free(struct declaration *declaration)
52 {
53 struct declaration_variant *variant_declaration =
54 container_of(declaration, struct declaration_variant, p);
55 unsigned long i;
56
57 free_declaration_scope(variant_declaration->scope);
58 g_hash_table_destroy(variant_declaration->fields_by_tag);
59
60 for (i = 0; i < variant_declaration->fields->len; i++) {
61 struct declaration_field *declaration_field =
62 &g_array_index(variant_declaration->fields,
63 struct declaration_field, i);
64 declaration_unref(declaration_field->declaration);
65 }
66 g_array_free(variant_declaration->fields, true);
67 g_free(variant_declaration);
68 }
69
70 struct declaration_variant *variant_declaration_new(const char *name,
71 struct declaration_scope *parent_scope)
72 {
73 struct declaration_variant *variant_declaration;
74 struct declaration *declaration;
75
76 variant_declaration = g_new(struct declaration_variant, 1);
77 declaration = &variant_declaration->p;
78 variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
79 g_direct_equal);
80 variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
81 sizeof(struct declaration_field),
82 DEFAULT_NR_STRUCT_FIELDS);
83 variant_declaration->scope = new_declaration_scope(parent_scope);
84 declaration->id = CTF_TYPE_VARIANT;
85 declaration->name = g_quark_from_string(name);
86 declaration->alignment = 1;
87 declaration->copy = variant_copy;
88 declaration->declaration_free = _variant_declaration_free;
89 declaration->definition_new = _variant_definition_new;
90 declaration->definition_free = _variant_definition_free;
91 declaration->ref = 1;
92 return variant_declaration;
93 }
94
95 static
96 struct definition *
97 _variant_definition_new(struct declaration *declaration,
98 struct definition_scope *parent_scope)
99 {
100 struct declaration_variant *variant_declaration =
101 container_of(declaration, struct declaration_variant, p);
102 struct definition_variant *variant;
103 unsigned long i;
104
105 variant = g_new(struct definition_variant, 1);
106 declaration_ref(&variant_declaration->p);
107 variant->p.declaration = declaration;
108 variant->declaration = variant_declaration;
109 variant->p.ref = 1;
110 variant->scope = new_definition_scope(parent_scope);
111 variant->fields = g_array_sized_new(FALSE, TRUE,
112 sizeof(struct field),
113 DEFAULT_NR_STRUCT_FIELDS);
114 g_array_set_size(variant->fields, variant_declaration->fields->len);
115 for (i = 0; i < variant_declaration->fields->len; i++) {
116 struct declaration_field *declaration_field =
117 &g_array_index(variant_declaration->fields,
118 struct declaration_field, i);
119 struct field *field = &g_array_index(variant->fields,
120 struct field, i);
121
122 field->name = declaration_field->name;
123 field->definition =
124 declaration_field->declaration->definition_new(declaration_field->declaration,
125 variant->scope);
126 }
127 variant->current_field = NULL;
128 return &variant->p;
129 }
130
131 static
132 void _variant_definition_free(struct definition *definition)
133 {
134 struct definition_variant *variant =
135 container_of(definition, struct definition_variant, p);
136 unsigned long i;
137
138 assert(variant->fields->len == variant->declaration->fields->len);
139 for (i = 0; i < variant->fields->len; i++) {
140 struct field *field = &g_array_index(variant->fields,
141 struct field, i);
142 definition_unref(field->definition);
143 }
144 free_definition_scope(variant->scope);
145 declaration_unref(variant->p.declaration);
146 g_free(variant);
147 }
148
149 void variant_declaration_add_field(struct declaration_variant *variant_declaration,
150 const char *tag_name,
151 struct declaration *tag_declaration)
152 {
153 struct declaration_field *field;
154 unsigned long index;
155
156 g_array_set_size(variant_declaration->fields, variant_declaration->fields->len + 1);
157 index = variant_declaration->fields->len - 1; /* last field (new) */
158 field = &g_array_index(variant_declaration->fields, struct declaration_field, index);
159 field->name = g_quark_from_string(tag_name);
160 declaration_ref(tag_declaration);
161 field->declaration = tag_declaration;
162 /* Keep index in hash rather than pointer, because array can relocate */
163 g_hash_table_insert(variant_declaration->fields_by_tag,
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
173 struct declaration_field *
174 struct_declaration_get_field_from_tag(struct declaration_variant *variant_declaration, GQuark tag)
175 {
176 unsigned long index;
177
178 index = (unsigned long) g_hash_table_lookup(variant_declaration->fields_by_tag,
179 (gconstpointer) (unsigned long) tag);
180 return &g_array_index(variant_declaration->fields, struct declaration_field, index);
181 }
182
183 /*
184 * tag_instance is assumed to be an enumeration.
185 */
186 int variant_definition_set_tag(struct definition_variant *variant,
187 struct definition *enum_tag)
188 {
189 struct definition_enum *_enum =
190 container_of(variant->enum_tag, struct definition_enum, p);
191 struct declaration_enum *enum_declaration = _enum->declaration;
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 */
201 if (variant->declaration->fields->len != enum_get_nr_enumerators(enum_declaration))
202 return -EPERM;
203
204 for (i = 0; i < variant->declaration->fields->len; i++) {
205 struct declaration_field *field_declaration =
206 &g_array_index(variant->declaration->fields,
207 struct declaration_field, i);
208 if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
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_declaration_get_current_field().
222 */
223
224 /* Set the enum tag field */
225 variant->enum_tag = enum_tag;
226 return 0;
227 }
228
229 /*
230 * field returned only valid as long as the field structure is not appended to.
231 */
232 struct field *variant_get_current_field(struct definition_variant *variant)
233 {
234 struct definition_enum *_enum =
235 container_of(variant->enum_tag, struct definition_enum, p);
236 struct declaration_variant *variant_declaration = variant->declaration;
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);
248 index = (unsigned long) g_hash_table_lookup(variant_declaration->fields_by_tag,
249 (gconstpointer) (unsigned long) tag);
250 variant->current_field = &g_array_index(variant->fields, struct field, index);
251 return variant->current_field;
252 }
This page took 0.086639 seconds and 4 git commands to generate.