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