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
);
41 fdest
->variant_begin(dest
, variant_declaration
);
43 field
= variant_get_current_field(variant
);
44 field_declaration
= field
->definition
->declaration
;
45 field_declaration
->copy(dest
, fdest
, src
, fsrc
, field
->definition
);
47 fsrc
->variant_end(src
, variant_declaration
);
48 fdest
->variant_end(dest
, variant_declaration
);
52 void _untagged_variant_declaration_free(struct declaration
*declaration
)
54 struct declaration_untagged_variant
*untagged_variant_declaration
=
55 container_of(declaration
, struct declaration_untagged_variant
, p
);
58 free_declaration_scope(untagged_variant_declaration
->scope
);
59 g_hash_table_destroy(untagged_variant_declaration
->fields_by_tag
);
61 for (i
= 0; i
< untagged_variant_declaration
->fields
->len
; i
++) {
62 struct declaration_field
*declaration_field
=
63 &g_array_index(untagged_variant_declaration
->fields
,
64 struct declaration_field
, i
);
65 declaration_unref(declaration_field
->declaration
);
67 g_array_free(untagged_variant_declaration
->fields
, true);
68 g_free(untagged_variant_declaration
);
71 struct declaration_untagged_variant
*untagged_variant_declaration_new(
72 struct declaration_scope
*parent_scope
)
74 struct declaration_untagged_variant
*untagged_variant_declaration
;
75 struct declaration
*declaration
;
77 untagged_variant_declaration
= g_new(struct declaration_untagged_variant
, 1);
78 declaration
= &untagged_variant_declaration
->p
;
79 untagged_variant_declaration
->fields_by_tag
= g_hash_table_new(g_direct_hash
,
81 untagged_variant_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
82 sizeof(struct declaration_field
),
83 DEFAULT_NR_STRUCT_FIELDS
);
84 untagged_variant_declaration
->scope
= new_declaration_scope(parent_scope
);
85 declaration
->id
= CTF_TYPE_UNTAGGED_VARIANT
;
86 declaration
->alignment
= 1;
87 declaration
->copy
= NULL
;
88 declaration
->declaration_free
= _untagged_variant_declaration_free
;
89 declaration
->definition_new
= NULL
;
90 declaration
->definition_free
= NULL
;
92 return untagged_variant_declaration
;
96 void _variant_declaration_free(struct declaration
*declaration
)
98 struct declaration_variant
*variant_declaration
=
99 container_of(declaration
, struct declaration_variant
, p
);
101 _untagged_variant_declaration_free(&variant_declaration
->untagged_variant
->p
);
102 g_array_free(variant_declaration
->tag_name
, TRUE
);
105 struct declaration_variant
*
106 variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
, const char *tag
)
108 struct declaration_variant
*variant_declaration
;
109 struct declaration
*declaration
;
111 variant_declaration
= g_new(struct declaration_variant
, 1);
112 declaration
= &variant_declaration
->p
;
113 variant_declaration
->untagged_variant
= untagged_variant
;
114 variant_declaration
->tag_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
115 append_scope_path(tag
, variant_declaration
->tag_name
);
116 declaration
->id
= CTF_TYPE_VARIANT
;
117 declaration
->alignment
= 1;
118 declaration
->copy
= variant_copy
;
119 declaration
->declaration_free
= _variant_declaration_free
;
120 declaration
->definition_new
= _variant_definition_new
;
121 declaration
->definition_free
= _variant_definition_free
;
122 declaration
->ref
= 1;
123 return variant_declaration
;
127 * tag_instance is assumed to be an enumeration.
128 * Returns 0 if OK, < 0 if error.
131 int check_enum_tag(struct definition_variant
*variant
,
132 struct definition
*enum_tag
)
134 struct definition_enum
*_enum
=
135 container_of(enum_tag
, struct definition_enum
, p
);
136 struct declaration_enum
*enum_declaration
= _enum
->declaration
;
137 int missing_field
= 0;
141 * Strictly speaking, each enumerator must map to a field of the
142 * variant. However, we are even stricter here by requiring that each
143 * variant choice map to an enumerator too. We then validate that the
144 * number of enumerators equals the number of variant choices.
146 if (variant
->declaration
->untagged_variant
->fields
->len
!= enum_get_nr_enumerators(enum_declaration
))
149 for (i
= 0; i
< variant
->declaration
->untagged_variant
->fields
->len
; i
++) {
150 struct declaration_field
*field_declaration
=
151 &g_array_index(variant
->declaration
->untagged_variant
->fields
,
152 struct declaration_field
, i
);
153 if (!enum_quark_to_range_set(enum_declaration
, field_declaration
->name
)) {
162 * Check the enumeration: it must map each value to one and only one
164 * TODO: we should also check that each range map to one and only one
165 * tag. For the moment, we will simply check this dynamically in
166 * variant_declaration_get_current_field().
175 _variant_definition_new(struct declaration
*declaration
,
176 struct definition_scope
*parent_scope
,
177 GQuark field_name
, int index
)
179 struct declaration_variant
*variant_declaration
=
180 container_of(declaration
, struct declaration_variant
, p
);
181 struct definition_variant
*variant
;
184 variant
= g_new(struct definition_variant
, 1);
185 declaration_ref(&variant_declaration
->p
);
186 variant
->p
.declaration
= declaration
;
187 variant
->declaration
= variant_declaration
;
189 variant
->p
.index
= index
;
190 variant
->scope
= new_definition_scope(parent_scope
, field_name
);
191 variant
->enum_tag
= lookup_definition(variant
->scope
->scope_path
,
192 variant_declaration
->tag_name
,
195 if (!variant
->enum_tag
196 || check_enum_tag(variant
, variant
->enum_tag
) < 0)
198 definition_ref(variant
->enum_tag
);
199 variant
->fields
= g_array_sized_new(FALSE
, TRUE
,
200 sizeof(struct field
),
201 variant_declaration
->untagged_variant
->fields
->len
);
202 g_array_set_size(variant
->fields
, variant_declaration
->untagged_variant
->fields
->len
);
203 for (i
= 0; i
< variant_declaration
->untagged_variant
->fields
->len
; i
++) {
204 struct declaration_field
*declaration_field
=
205 &g_array_index(variant_declaration
->untagged_variant
->fields
,
206 struct declaration_field
, i
);
207 struct field
*field
= &g_array_index(variant
->fields
,
210 field
->name
= declaration_field
->name
;
212 * All child definition are at index 0, because they are
213 * various choices of the same field.
216 declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
220 variant
->current_field
= NULL
;
223 free_definition_scope(variant
->scope
);
224 declaration_unref(&variant_declaration
->p
);
230 void _variant_definition_free(struct definition
*definition
)
232 struct definition_variant
*variant
=
233 container_of(definition
, struct definition_variant
, p
);
236 assert(variant
->fields
->len
== variant
->declaration
->untagged_variant
->fields
->len
);
237 for (i
= 0; i
< variant
->fields
->len
; i
++) {
238 struct field
*field
= &g_array_index(variant
->fields
,
240 definition_unref(field
->definition
);
242 definition_unref(variant
->enum_tag
);
243 free_definition_scope(variant
->scope
);
244 declaration_unref(variant
->p
.declaration
);
248 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
249 const char *field_name
,
250 struct declaration
*field_declaration
)
252 struct declaration_field
*field
;
255 g_array_set_size(untagged_variant_declaration
->fields
, untagged_variant_declaration
->fields
->len
+ 1);
256 index
= untagged_variant_declaration
->fields
->len
- 1; /* last field (new) */
257 field
= &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
258 field
->name
= g_quark_from_string(field_name
);
259 declaration_ref(field_declaration
);
260 field
->declaration
= field_declaration
;
261 /* Keep index in hash rather than pointer, because array can relocate */
262 g_hash_table_insert(untagged_variant_declaration
->fields_by_tag
,
263 (gpointer
) (unsigned long) field
->name
,
266 * Alignment of variant is based on the alignment of its currently
267 * selected choice, so we leave variant alignment as-is (statically
272 struct declaration_field
*
273 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
, GQuark tag
)
277 index
= (unsigned long) g_hash_table_lookup(untagged_variant_declaration
->fields_by_tag
,
278 (gconstpointer
) (unsigned long) tag
);
279 return &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
283 * field returned only valid as long as the field structure is not appended to.
285 struct field
*variant_get_current_field(struct definition_variant
*variant
)
287 struct definition_enum
*_enum
=
288 container_of(variant
->enum_tag
, struct definition_enum
, p
);
289 struct declaration_variant
*variant_declaration
= variant
->declaration
;
294 tag_array
= _enum
->value
;
296 * The 1 to 1 mapping from enumeration to value should have been already
297 * checked. (see TODO above)
299 assert(tag_array
->len
== 1);
300 tag
= g_array_index(tag_array
, GQuark
, 0);
301 index
= (unsigned long) g_hash_table_lookup(variant_declaration
->untagged_variant
->fields_by_tag
,
302 (gconstpointer
) (unsigned long) tag
);
303 variant
->current_field
= &g_array_index(variant
->fields
, struct field
, index
);
304 return variant
->current_field
;