4 * BabelTrace - Variant Type Converter
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
24 struct definition
*_variant_definition_new(struct declaration
*declaration
,
25 struct definition_scope
*parent_scope
,
26 GQuark field_name
, int index
);
28 void _variant_definition_free(struct definition
*definition
);
30 void variant_copy(struct stream_pos
*dest
, const struct format
*fdest
,
31 struct stream_pos
*src
, const struct format
*fsrc
,
32 struct definition
*definition
)
34 struct definition_variant
*variant
=
35 container_of(definition
, struct definition_variant
, p
);
36 struct declaration_variant
*variant_declaration
= variant
->declaration
;
38 struct declaration
*field_declaration
;
40 fsrc
->variant_begin(src
, variant_declaration
);
42 fdest
->variant_begin(dest
, variant_declaration
);
44 field
= variant_get_current_field(variant
);
45 field_declaration
= field
->definition
->declaration
;
46 field_declaration
->copy(dest
, fdest
, src
, fsrc
, field
->definition
);
48 fsrc
->variant_end(src
, variant_declaration
);
50 fdest
->variant_end(dest
, variant_declaration
);
54 void _untagged_variant_declaration_free(struct declaration
*declaration
)
56 struct declaration_untagged_variant
*untagged_variant_declaration
=
57 container_of(declaration
, struct declaration_untagged_variant
, p
);
60 free_declaration_scope(untagged_variant_declaration
->scope
);
61 g_hash_table_destroy(untagged_variant_declaration
->fields_by_tag
);
63 for (i
= 0; i
< untagged_variant_declaration
->fields
->len
; i
++) {
64 struct declaration_field
*declaration_field
=
65 &g_array_index(untagged_variant_declaration
->fields
,
66 struct declaration_field
, i
);
67 declaration_unref(declaration_field
->declaration
);
69 g_array_free(untagged_variant_declaration
->fields
, true);
70 g_free(untagged_variant_declaration
);
73 struct declaration_untagged_variant
*untagged_variant_declaration_new(
74 struct declaration_scope
*parent_scope
)
76 struct declaration_untagged_variant
*untagged_variant_declaration
;
77 struct declaration
*declaration
;
79 untagged_variant_declaration
= g_new(struct declaration_untagged_variant
, 1);
80 declaration
= &untagged_variant_declaration
->p
;
81 untagged_variant_declaration
->fields_by_tag
= g_hash_table_new(g_direct_hash
,
83 untagged_variant_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
84 sizeof(struct declaration_field
),
85 DEFAULT_NR_STRUCT_FIELDS
);
86 untagged_variant_declaration
->scope
= new_declaration_scope(parent_scope
);
87 declaration
->id
= CTF_TYPE_UNTAGGED_VARIANT
;
88 declaration
->alignment
= 1;
89 declaration
->copy
= NULL
;
90 declaration
->declaration_free
= _untagged_variant_declaration_free
;
91 declaration
->definition_new
= NULL
;
92 declaration
->definition_free
= NULL
;
94 return untagged_variant_declaration
;
98 void _variant_declaration_free(struct declaration
*declaration
)
100 struct declaration_variant
*variant_declaration
=
101 container_of(declaration
, struct declaration_variant
, p
);
103 _untagged_variant_declaration_free(&variant_declaration
->untagged_variant
->p
);
104 g_array_free(variant_declaration
->tag_name
, TRUE
);
107 struct declaration_variant
*
108 variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
, const char *tag
)
110 struct declaration_variant
*variant_declaration
;
111 struct declaration
*declaration
;
113 variant_declaration
= g_new(struct declaration_variant
, 1);
114 declaration
= &variant_declaration
->p
;
115 variant_declaration
->untagged_variant
= untagged_variant
;
116 variant_declaration
->tag_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
117 append_scope_path(tag
, variant_declaration
->tag_name
);
118 declaration
->id
= CTF_TYPE_VARIANT
;
119 declaration
->alignment
= 1;
120 declaration
->copy
= variant_copy
;
121 declaration
->declaration_free
= _variant_declaration_free
;
122 declaration
->definition_new
= _variant_definition_new
;
123 declaration
->definition_free
= _variant_definition_free
;
124 declaration
->ref
= 1;
125 return variant_declaration
;
129 * tag_instance is assumed to be an enumeration.
130 * Returns 0 if OK, < 0 if error.
133 int check_enum_tag(struct definition_variant
*variant
,
134 struct definition
*enum_tag
)
136 struct definition_enum
*_enum
=
137 container_of(enum_tag
, struct definition_enum
, p
);
138 struct declaration_enum
*enum_declaration
= _enum
->declaration
;
139 int missing_field
= 0;
143 * Strictly speaking, each enumerator must map to a field of the
144 * variant. However, we are even stricter here by requiring that each
145 * variant choice map to an enumerator too. We then validate that the
146 * number of enumerators equals the number of variant choices.
148 if (variant
->declaration
->untagged_variant
->fields
->len
!= enum_get_nr_enumerators(enum_declaration
))
151 for (i
= 0; i
< variant
->declaration
->untagged_variant
->fields
->len
; i
++) {
152 struct declaration_field
*field_declaration
=
153 &g_array_index(variant
->declaration
->untagged_variant
->fields
,
154 struct declaration_field
, i
);
155 if (!enum_quark_to_range_set(enum_declaration
, field_declaration
->name
)) {
164 * Check the enumeration: it must map each value to one and only one
166 * TODO: we should also check that each range map to one and only one
167 * tag. For the moment, we will simply check this dynamically in
168 * variant_declaration_get_current_field().
177 _variant_definition_new(struct declaration
*declaration
,
178 struct definition_scope
*parent_scope
,
179 GQuark field_name
, int index
)
181 struct declaration_variant
*variant_declaration
=
182 container_of(declaration
, struct declaration_variant
, p
);
183 struct definition_variant
*variant
;
186 variant
= g_new(struct definition_variant
, 1);
187 declaration_ref(&variant_declaration
->p
);
188 variant
->p
.declaration
= declaration
;
189 variant
->declaration
= variant_declaration
;
191 variant
->p
.index
= index
;
192 variant
->scope
= new_definition_scope(parent_scope
, field_name
);
193 variant
->enum_tag
= lookup_definition(variant
->scope
->scope_path
,
194 variant_declaration
->tag_name
,
197 if (!variant
->enum_tag
198 || check_enum_tag(variant
, variant
->enum_tag
) < 0)
200 definition_ref(variant
->enum_tag
);
201 variant
->fields
= g_array_sized_new(FALSE
, TRUE
,
202 sizeof(struct field
),
203 variant_declaration
->untagged_variant
->fields
->len
);
204 g_array_set_size(variant
->fields
, variant_declaration
->untagged_variant
->fields
->len
);
205 for (i
= 0; i
< variant_declaration
->untagged_variant
->fields
->len
; i
++) {
206 struct declaration_field
*declaration_field
=
207 &g_array_index(variant_declaration
->untagged_variant
->fields
,
208 struct declaration_field
, i
);
209 struct field
*field
= &g_array_index(variant
->fields
,
212 field
->name
= declaration_field
->name
;
214 * All child definition are at index 0, because they are
215 * various choices of the same field.
218 declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
222 variant
->current_field
= NULL
;
225 free_definition_scope(variant
->scope
);
226 declaration_unref(&variant_declaration
->p
);
232 void _variant_definition_free(struct definition
*definition
)
234 struct definition_variant
*variant
=
235 container_of(definition
, struct definition_variant
, p
);
238 assert(variant
->fields
->len
== variant
->declaration
->untagged_variant
->fields
->len
);
239 for (i
= 0; i
< variant
->fields
->len
; i
++) {
240 struct field
*field
= &g_array_index(variant
->fields
,
242 definition_unref(field
->definition
);
244 definition_unref(variant
->enum_tag
);
245 free_definition_scope(variant
->scope
);
246 declaration_unref(variant
->p
.declaration
);
250 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
251 const char *field_name
,
252 struct declaration
*field_declaration
)
254 struct declaration_field
*field
;
257 g_array_set_size(untagged_variant_declaration
->fields
, untagged_variant_declaration
->fields
->len
+ 1);
258 index
= untagged_variant_declaration
->fields
->len
- 1; /* last field (new) */
259 field
= &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
260 field
->name
= g_quark_from_string(field_name
);
261 declaration_ref(field_declaration
);
262 field
->declaration
= field_declaration
;
263 /* Keep index in hash rather than pointer, because array can relocate */
264 g_hash_table_insert(untagged_variant_declaration
->fields_by_tag
,
265 (gpointer
) (unsigned long) field
->name
,
268 * Alignment of variant is based on the alignment of its currently
269 * selected choice, so we leave variant alignment as-is (statically
274 struct declaration_field
*
275 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
, GQuark tag
)
279 index
= (unsigned long) g_hash_table_lookup(untagged_variant_declaration
->fields_by_tag
,
280 (gconstpointer
) (unsigned long) tag
);
281 return &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
285 * field returned only valid as long as the field structure is not appended to.
287 struct field
*variant_get_current_field(struct definition_variant
*variant
)
289 struct definition_enum
*_enum
=
290 container_of(variant
->enum_tag
, struct definition_enum
, p
);
291 struct declaration_variant
*variant_declaration
= variant
->declaration
;
296 tag_array
= _enum
->value
;
298 * The 1 to 1 mapping from enumeration to value should have been already
299 * checked. (see TODO above)
301 assert(tag_array
->len
== 1);
302 tag
= g_array_index(tag_array
, GQuark
, 0);
303 index
= (unsigned long) g_hash_table_lookup(variant_declaration
->untagged_variant
->fields_by_tag
,
304 (gconstpointer
) (unsigned long) tag
);
305 variant
->current_field
= &g_array_index(variant
->fields
, struct field
, index
);
306 return variant
->current_field
;