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(const char *name
,
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
->name
= g_quark_from_string(name
);
87 declaration
->alignment
= 1;
88 declaration
->copy
= NULL
;
89 declaration
->declaration_free
= _untagged_variant_declaration_free
;
90 declaration
->definition_new
= NULL
;
91 declaration
->definition_free
= NULL
;
93 return untagged_variant_declaration
;
97 void _variant_declaration_free(struct declaration
*declaration
)
99 struct declaration_variant
*variant_declaration
=
100 container_of(declaration
, struct declaration_variant
, p
);
102 _untagged_variant_declaration_free(&variant_declaration
->untagged_variant
->p
);
103 g_array_free(variant_declaration
->tag_name
, TRUE
);
106 struct declaration_variant
*
107 variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
, const char *tag
)
109 struct declaration_variant
*variant_declaration
;
110 struct declaration
*declaration
;
112 variant_declaration
= g_new(struct declaration_variant
, 1);
113 declaration
= &variant_declaration
->p
;
114 variant_declaration
->untagged_variant
= untagged_variant
;
115 variant_declaration
->tag_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
116 append_scope_path(tag
, variant_declaration
->tag_name
);
117 declaration
->id
= CTF_TYPE_VARIANT
;
118 declaration
->alignment
= 1;
119 declaration
->copy
= variant_copy
;
120 declaration
->declaration_free
= _variant_declaration_free
;
121 declaration
->definition_new
= _variant_definition_new
;
122 declaration
->definition_free
= _variant_definition_free
;
123 declaration
->ref
= 1;
124 return variant_declaration
;
128 * tag_instance is assumed to be an enumeration.
129 * Returns 0 if OK, < 0 if error.
132 int check_enum_tag(struct definition_variant
*variant
,
133 struct definition
*enum_tag
)
135 struct definition_enum
*_enum
=
136 container_of(enum_tag
, struct definition_enum
, p
);
137 struct declaration_enum
*enum_declaration
= _enum
->declaration
;
138 int missing_field
= 0;
142 * Strictly speaking, each enumerator must map to a field of the
143 * variant. However, we are even stricter here by requiring that each
144 * variant choice map to an enumerator too. We then validate that the
145 * number of enumerators equals the number of variant choices.
147 if (variant
->declaration
->untagged_variant
->fields
->len
!= enum_get_nr_enumerators(enum_declaration
))
150 for (i
= 0; i
< variant
->declaration
->untagged_variant
->fields
->len
; i
++) {
151 struct declaration_field
*field_declaration
=
152 &g_array_index(variant
->declaration
->untagged_variant
->fields
,
153 struct declaration_field
, i
);
154 if (!enum_quark_to_range_set(enum_declaration
, field_declaration
->name
)) {
163 * Check the enumeration: it must map each value to one and only one
165 * TODO: we should also check that each range map to one and only one
166 * tag. For the moment, we will simply check this dynamically in
167 * variant_declaration_get_current_field().
176 _variant_definition_new(struct declaration
*declaration
,
177 struct definition_scope
*parent_scope
,
178 GQuark field_name
, int index
)
180 struct declaration_variant
*variant_declaration
=
181 container_of(declaration
, struct declaration_variant
, p
);
182 struct definition_variant
*variant
;
185 variant
= g_new(struct definition_variant
, 1);
186 declaration_ref(&variant_declaration
->p
);
187 variant
->p
.declaration
= declaration
;
188 variant
->declaration
= variant_declaration
;
190 variant
->p
.index
= index
;
191 variant
->scope
= new_definition_scope(parent_scope
, field_name
);
192 variant
->enum_tag
= lookup_definition(variant
->scope
->scope_path
,
193 variant_declaration
->tag_name
,
196 if (!variant
->enum_tag
197 || check_enum_tag(variant
, variant
->enum_tag
) < 0)
199 definition_ref(variant
->enum_tag
);
200 variant
->fields
= g_array_sized_new(FALSE
, TRUE
,
201 sizeof(struct field
),
202 variant_declaration
->untagged_variant
->fields
->len
);
203 g_array_set_size(variant
->fields
, variant_declaration
->untagged_variant
->fields
->len
);
204 for (i
= 0; i
< variant_declaration
->untagged_variant
->fields
->len
; i
++) {
205 struct declaration_field
*declaration_field
=
206 &g_array_index(variant_declaration
->untagged_variant
->fields
,
207 struct declaration_field
, i
);
208 struct field
*field
= &g_array_index(variant
->fields
,
211 field
->name
= declaration_field
->name
;
213 * All child definition are at index 0, because they are
214 * various choices of the same field.
217 declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
221 variant
->current_field
= NULL
;
224 free_definition_scope(variant
->scope
);
225 declaration_unref(&variant_declaration
->p
);
231 void _variant_definition_free(struct definition
*definition
)
233 struct definition_variant
*variant
=
234 container_of(definition
, struct definition_variant
, p
);
237 assert(variant
->fields
->len
== variant
->declaration
->untagged_variant
->fields
->len
);
238 for (i
= 0; i
< variant
->fields
->len
; i
++) {
239 struct field
*field
= &g_array_index(variant
->fields
,
241 definition_unref(field
->definition
);
243 definition_unref(variant
->enum_tag
);
244 free_definition_scope(variant
->scope
);
245 declaration_unref(variant
->p
.declaration
);
249 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
250 const char *field_name
,
251 struct declaration
*field_declaration
)
253 struct declaration_field
*field
;
256 g_array_set_size(untagged_variant_declaration
->fields
, untagged_variant_declaration
->fields
->len
+ 1);
257 index
= untagged_variant_declaration
->fields
->len
- 1; /* last field (new) */
258 field
= &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
259 field
->name
= g_quark_from_string(field_name
);
260 declaration_ref(field_declaration
);
261 field
->declaration
= field_declaration
;
262 /* Keep index in hash rather than pointer, because array can relocate */
263 g_hash_table_insert(untagged_variant_declaration
->fields_by_tag
,
264 (gpointer
) (unsigned long) field
->name
,
267 * Alignment of variant is based on the alignment of its currently
268 * selected choice, so we leave variant alignment as-is (statically
273 struct declaration_field
*
274 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
, GQuark tag
)
278 index
= (unsigned long) g_hash_table_lookup(untagged_variant_declaration
->fields_by_tag
,
279 (gconstpointer
) (unsigned long) tag
);
280 return &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
284 * field returned only valid as long as the field structure is not appended to.
286 struct field
*variant_get_current_field(struct definition_variant
*variant
)
288 struct definition_enum
*_enum
=
289 container_of(variant
->enum_tag
, struct definition_enum
, p
);
290 struct declaration_variant
*variant_declaration
= variant
->declaration
;
295 tag_array
= _enum
->value
;
297 * The 1 to 1 mapping from enumeration to value should have been already
298 * checked. (see TODO above)
300 assert(tag_array
->len
== 1);
301 tag
= g_array_index(tag_array
, GQuark
, 0);
302 index
= (unsigned long) g_hash_table_lookup(variant_declaration
->untagged_variant
->fields_by_tag
,
303 (gconstpointer
) (unsigned long) tag
);
304 variant
->current_field
= &g_array_index(variant
->fields
, struct field
, index
);
305 return variant
->current_field
;