4 * BabelTrace - Variant Type Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/compiler.h>
22 #include <babeltrace/format.h>
26 struct definition
*_variant_definition_new(struct declaration
*declaration
,
27 struct definition_scope
*parent_scope
,
28 GQuark field_name
, int index
,
29 const char *root_name
);
31 void _variant_definition_free(struct definition
*definition
);
33 int variant_rw(struct stream_pos
*ppos
, struct definition
*definition
)
35 struct definition_variant
*variant_definition
=
36 container_of(definition
, struct definition_variant
, p
);
37 struct definition
*field
;
39 field
= variant_get_current_field(variant_definition
);
40 return generic_rw(ppos
, field
);
44 void _untagged_variant_declaration_free(struct declaration
*declaration
)
46 struct declaration_untagged_variant
*untagged_variant_declaration
=
47 container_of(declaration
, struct declaration_untagged_variant
, p
);
50 free_declaration_scope(untagged_variant_declaration
->scope
);
51 g_hash_table_destroy(untagged_variant_declaration
->fields_by_tag
);
53 for (i
= 0; i
< untagged_variant_declaration
->fields
->len
; i
++) {
54 struct declaration_field
*declaration_field
=
55 &g_array_index(untagged_variant_declaration
->fields
,
56 struct declaration_field
, i
);
57 declaration_unref(declaration_field
->declaration
);
59 g_array_free(untagged_variant_declaration
->fields
, true);
60 g_free(untagged_variant_declaration
);
63 struct declaration_untagged_variant
*untagged_variant_declaration_new(
64 struct declaration_scope
*parent_scope
)
66 struct declaration_untagged_variant
*untagged_variant_declaration
;
67 struct declaration
*declaration
;
69 untagged_variant_declaration
= g_new(struct declaration_untagged_variant
, 1);
70 declaration
= &untagged_variant_declaration
->p
;
71 untagged_variant_declaration
->fields_by_tag
= g_hash_table_new(g_direct_hash
,
73 untagged_variant_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
74 sizeof(struct declaration_field
),
75 DEFAULT_NR_STRUCT_FIELDS
);
76 untagged_variant_declaration
->scope
= new_declaration_scope(parent_scope
);
77 declaration
->id
= CTF_TYPE_UNTAGGED_VARIANT
;
78 declaration
->alignment
= 1;
79 declaration
->declaration_free
= _untagged_variant_declaration_free
;
80 declaration
->definition_new
= NULL
;
81 declaration
->definition_free
= NULL
;
83 return untagged_variant_declaration
;
87 void _variant_declaration_free(struct declaration
*declaration
)
89 struct declaration_variant
*variant_declaration
=
90 container_of(declaration
, struct declaration_variant
, p
);
92 _untagged_variant_declaration_free(&variant_declaration
->untagged_variant
->p
);
93 g_array_free(variant_declaration
->tag_name
, TRUE
);
96 struct declaration_variant
*
97 variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
, const char *tag
)
99 struct declaration_variant
*variant_declaration
;
100 struct declaration
*declaration
;
102 variant_declaration
= g_new(struct declaration_variant
, 1);
103 declaration
= &variant_declaration
->p
;
104 variant_declaration
->untagged_variant
= untagged_variant
;
105 declaration_ref(&untagged_variant
->p
);
106 variant_declaration
->tag_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
107 append_scope_path(tag
, variant_declaration
->tag_name
);
108 declaration
->id
= CTF_TYPE_VARIANT
;
109 declaration
->alignment
= 1;
110 declaration
->declaration_free
= _variant_declaration_free
;
111 declaration
->definition_new
= _variant_definition_new
;
112 declaration
->definition_free
= _variant_definition_free
;
113 declaration
->ref
= 1;
114 return variant_declaration
;
118 * tag_instance is assumed to be an enumeration.
119 * Returns 0 if OK, < 0 if error.
122 int check_enum_tag(struct definition_variant
*variant
,
123 struct definition
*enum_tag
)
125 struct definition_enum
*_enum
=
126 container_of(enum_tag
, struct definition_enum
, p
);
127 struct declaration_enum
*enum_declaration
= _enum
->declaration
;
128 int missing_field
= 0;
132 * Strictly speaking, each enumerator must map to a field of the
133 * variant. However, we are even stricter here by requiring that each
134 * variant choice map to an enumerator too. We then validate that the
135 * number of enumerators equals the number of variant choices.
137 if (variant
->declaration
->untagged_variant
->fields
->len
!= enum_get_nr_enumerators(enum_declaration
))
140 for (i
= 0; i
< variant
->declaration
->untagged_variant
->fields
->len
; i
++) {
141 struct declaration_field
*field_declaration
=
142 &g_array_index(variant
->declaration
->untagged_variant
->fields
,
143 struct declaration_field
, i
);
144 if (!enum_quark_to_range_set(enum_declaration
, field_declaration
->name
)) {
153 * Check the enumeration: it must map each value to one and only one
155 * TODO: we should also check that each range map to one and only one
156 * tag. For the moment, we will simply check this dynamically in
157 * variant_declaration_get_current_field().
166 _variant_definition_new(struct declaration
*declaration
,
167 struct definition_scope
*parent_scope
,
168 GQuark field_name
, int index
,
169 const char *root_name
)
171 struct declaration_variant
*variant_declaration
=
172 container_of(declaration
, struct declaration_variant
, p
);
173 struct definition_variant
*variant
;
177 variant
= g_new(struct definition_variant
, 1);
178 declaration_ref(&variant_declaration
->p
);
179 variant
->p
.declaration
= declaration
;
180 variant
->declaration
= variant_declaration
;
183 * Use INT_MAX order to ensure that all fields of the parent
184 * scope are seen as being prior to this scope.
186 variant
->p
.index
= root_name
? INT_MAX
: index
;
187 variant
->p
.name
= field_name
;
188 variant
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
189 variant
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
191 ret
= register_field_definition(field_name
, &variant
->p
,
195 variant
->enum_tag
= lookup_path_definition(variant
->p
.scope
->scope_path
,
196 variant_declaration
->tag_name
,
199 if (!variant
->enum_tag
200 || check_enum_tag(variant
, variant
->enum_tag
) < 0)
202 definition_ref(variant
->enum_tag
);
203 variant
->fields
= g_ptr_array_sized_new(variant_declaration
->untagged_variant
->fields
->len
);
204 g_ptr_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 definition
**field
=
210 (struct definition
**) &g_ptr_array_index(variant
->fields
, i
);
213 * All child definition are at index 0, because they are
214 * various choices of the same field.
216 *field
= declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
218 declaration_field
->name
, 0, NULL
);
222 variant
->current_field
= NULL
;
225 free_definition_scope(variant
->p
.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 definition
*field
= g_ptr_array_index(variant
->fields
, i
);
241 definition_unref(field
);
243 definition_unref(variant
->enum_tag
);
244 free_definition_scope(variant
->p
.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
)
279 found
= g_hash_table_lookup_extended(
280 untagged_variant_declaration
->fields_by_tag
,
281 (gconstpointer
) (unsigned long) tag
, NULL
, &index
);
287 return &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, (unsigned long)index
);
291 * field returned only valid as long as the field structure is not appended to.
293 struct definition
*variant_get_current_field(struct definition_variant
*variant
)
295 struct definition_enum
*_enum
=
296 container_of(variant
->enum_tag
, struct definition_enum
, p
);
297 struct declaration_variant
*variant_declaration
= variant
->declaration
;
302 tag_array
= _enum
->value
;
304 * The 1 to 1 mapping from enumeration to value should have been already
305 * checked. (see TODO above)
307 assert(tag_array
->len
== 1);
308 tag
= g_array_index(tag_array
, GQuark
, 0);
309 index
= (unsigned long) g_hash_table_lookup(variant_declaration
->untagged_variant
->fields_by_tag
,
310 (gconstpointer
) (unsigned long) tag
);
311 variant
->current_field
= g_ptr_array_index(variant
->fields
, index
);
312 return variant
->current_field
;