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>
23 #include <babeltrace/types.h>
27 struct definition
*_variant_definition_new(struct declaration
*declaration
,
28 struct definition_scope
*parent_scope
,
29 GQuark field_name
, int index
,
30 const char *root_name
);
32 void _variant_definition_free(struct definition
*definition
);
34 int variant_rw(struct stream_pos
*ppos
, struct definition
*definition
)
36 struct definition_variant
*variant_definition
=
37 container_of(definition
, struct definition_variant
, p
);
38 struct definition
*field
;
40 field
= variant_get_current_field(variant_definition
);
41 return generic_rw(ppos
, field
);
45 void _untagged_variant_declaration_free(struct declaration
*declaration
)
47 struct declaration_untagged_variant
*untagged_variant_declaration
=
48 container_of(declaration
, struct declaration_untagged_variant
, p
);
51 free_declaration_scope(untagged_variant_declaration
->scope
);
52 g_hash_table_destroy(untagged_variant_declaration
->fields_by_tag
);
54 for (i
= 0; i
< untagged_variant_declaration
->fields
->len
; i
++) {
55 struct declaration_field
*declaration_field
=
56 &g_array_index(untagged_variant_declaration
->fields
,
57 struct declaration_field
, i
);
58 declaration_unref(declaration_field
->declaration
);
60 g_array_free(untagged_variant_declaration
->fields
, true);
61 g_free(untagged_variant_declaration
);
64 struct declaration_untagged_variant
*untagged_variant_declaration_new(
65 struct declaration_scope
*parent_scope
)
67 struct declaration_untagged_variant
*untagged_variant_declaration
;
68 struct declaration
*declaration
;
70 untagged_variant_declaration
= g_new(struct declaration_untagged_variant
, 1);
71 declaration
= &untagged_variant_declaration
->p
;
72 untagged_variant_declaration
->fields_by_tag
= g_hash_table_new(g_direct_hash
,
74 untagged_variant_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
75 sizeof(struct declaration_field
),
76 DEFAULT_NR_STRUCT_FIELDS
);
77 untagged_variant_declaration
->scope
= new_declaration_scope(parent_scope
);
78 declaration
->id
= CTF_TYPE_UNTAGGED_VARIANT
;
79 declaration
->alignment
= 1;
80 declaration
->declaration_free
= _untagged_variant_declaration_free
;
81 declaration
->definition_new
= NULL
;
82 declaration
->definition_free
= NULL
;
84 return untagged_variant_declaration
;
88 void _variant_declaration_free(struct declaration
*declaration
)
90 struct declaration_variant
*variant_declaration
=
91 container_of(declaration
, struct declaration_variant
, p
);
93 declaration_unref(&variant_declaration
->untagged_variant
->p
);
94 g_array_free(variant_declaration
->tag_name
, TRUE
);
95 g_free(variant_declaration
);
98 struct declaration_variant
*
99 variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
, const char *tag
)
101 struct declaration_variant
*variant_declaration
;
102 struct declaration
*declaration
;
104 variant_declaration
= g_new(struct declaration_variant
, 1);
105 declaration
= &variant_declaration
->p
;
106 variant_declaration
->untagged_variant
= untagged_variant
;
107 declaration_ref(&untagged_variant
->p
);
108 variant_declaration
->tag_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
109 append_scope_path(tag
, variant_declaration
->tag_name
);
110 declaration
->id
= CTF_TYPE_VARIANT
;
111 declaration
->alignment
= 1;
112 declaration
->declaration_free
= _variant_declaration_free
;
113 declaration
->definition_new
= _variant_definition_new
;
114 declaration
->definition_free
= _variant_definition_free
;
115 declaration
->ref
= 1;
116 return variant_declaration
;
120 * tag_instance is assumed to be an enumeration.
121 * Returns 0 if OK, < 0 if error.
124 int check_enum_tag(struct definition_variant
*variant
,
125 struct definition
*enum_tag
)
127 struct definition_enum
*_enum
=
128 container_of(enum_tag
, struct definition_enum
, p
);
129 struct declaration_enum
*enum_declaration
= _enum
->declaration
;
130 int missing_field
= 0;
134 * Strictly speaking, each enumerator must map to a field of the
135 * variant. However, we are even stricter here by requiring that each
136 * variant choice map to an enumerator too. We then validate that the
137 * number of enumerators equals the number of variant choices.
139 if (variant
->declaration
->untagged_variant
->fields
->len
!= enum_get_nr_enumerators(enum_declaration
))
142 for (i
= 0; i
< variant
->declaration
->untagged_variant
->fields
->len
; i
++) {
143 struct declaration_field
*field_declaration
=
144 &g_array_index(variant
->declaration
->untagged_variant
->fields
,
145 struct declaration_field
, i
);
146 if (!enum_quark_to_range_set(enum_declaration
, field_declaration
->name
)) {
155 * Check the enumeration: it must map each value to one and only one
157 * TODO: we should also check that each range map to one and only one
158 * tag. For the moment, we will simply check this dynamically in
159 * variant_declaration_get_current_field().
168 _variant_definition_new(struct declaration
*declaration
,
169 struct definition_scope
*parent_scope
,
170 GQuark field_name
, int index
,
171 const char *root_name
)
173 struct declaration_variant
*variant_declaration
=
174 container_of(declaration
, struct declaration_variant
, p
);
175 struct definition_variant
*variant
;
179 variant
= g_new(struct definition_variant
, 1);
180 declaration_ref(&variant_declaration
->p
);
181 variant
->p
.declaration
= declaration
;
182 variant
->declaration
= variant_declaration
;
185 * Use INT_MAX order to ensure that all fields of the parent
186 * scope are seen as being prior to this scope.
188 variant
->p
.index
= root_name
? INT_MAX
: index
;
189 variant
->p
.name
= field_name
;
190 variant
->p
.path
= new_definition_path(parent_scope
, field_name
, root_name
);
191 variant
->p
.scope
= new_definition_scope(parent_scope
, field_name
, root_name
);
193 ret
= register_field_definition(field_name
, &variant
->p
,
197 variant
->enum_tag
= lookup_path_definition(variant
->p
.scope
->scope_path
,
198 variant_declaration
->tag_name
,
201 if (!variant
->enum_tag
202 || check_enum_tag(variant
, variant
->enum_tag
) < 0)
204 definition_ref(variant
->enum_tag
);
205 variant
->fields
= g_ptr_array_sized_new(variant_declaration
->untagged_variant
->fields
->len
);
206 g_ptr_array_set_size(variant
->fields
, variant_declaration
->untagged_variant
->fields
->len
);
207 for (i
= 0; i
< variant_declaration
->untagged_variant
->fields
->len
; i
++) {
208 struct declaration_field
*declaration_field
=
209 &g_array_index(variant_declaration
->untagged_variant
->fields
,
210 struct declaration_field
, i
);
211 struct definition
**field
=
212 (struct definition
**) &g_ptr_array_index(variant
->fields
, i
);
215 * All child definition are at index 0, because they are
216 * various choices of the same field.
218 *field
= declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
220 declaration_field
->name
, 0, NULL
);
224 variant
->current_field
= NULL
;
227 free_definition_scope(variant
->p
.scope
);
228 declaration_unref(&variant_declaration
->p
);
234 void _variant_definition_free(struct definition
*definition
)
236 struct definition_variant
*variant
=
237 container_of(definition
, struct definition_variant
, p
);
240 assert(variant
->fields
->len
== variant
->declaration
->untagged_variant
->fields
->len
);
241 for (i
= 0; i
< variant
->fields
->len
; i
++) {
242 struct definition
*field
= g_ptr_array_index(variant
->fields
, i
);
243 definition_unref(field
);
245 definition_unref(variant
->enum_tag
);
246 free_definition_scope(variant
->p
.scope
);
247 declaration_unref(variant
->p
.declaration
);
248 g_ptr_array_free(variant
->fields
, TRUE
);
252 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
253 const char *field_name
,
254 struct declaration
*field_declaration
)
256 struct declaration_field
*field
;
259 g_array_set_size(untagged_variant_declaration
->fields
, untagged_variant_declaration
->fields
->len
+ 1);
260 index
= untagged_variant_declaration
->fields
->len
- 1; /* last field (new) */
261 field
= &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
262 field
->name
= g_quark_from_string(field_name
);
263 declaration_ref(field_declaration
);
264 field
->declaration
= field_declaration
;
265 /* Keep index in hash rather than pointer, because array can relocate */
266 g_hash_table_insert(untagged_variant_declaration
->fields_by_tag
,
267 (gpointer
) (unsigned long) field
->name
,
270 * Alignment of variant is based on the alignment of its currently
271 * selected choice, so we leave variant alignment as-is (statically
276 struct declaration_field
*
277 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
, GQuark tag
)
282 found
= g_hash_table_lookup_extended(
283 untagged_variant_declaration
->fields_by_tag
,
284 (gconstpointer
) (unsigned long) tag
, NULL
, &index
);
290 return &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, (unsigned long)index
);
294 * field returned only valid as long as the field structure is not appended to.
296 struct definition
*variant_get_current_field(struct definition_variant
*variant
)
298 struct definition_enum
*_enum
=
299 container_of(variant
->enum_tag
, struct definition_enum
, p
);
300 struct declaration_variant
*variant_declaration
= variant
->declaration
;
305 tag_array
= _enum
->value
;
307 * The 1 to 1 mapping from enumeration to value should have been already
308 * checked. (see TODO above)
310 assert(tag_array
->len
== 1);
311 tag
= g_array_index(tag_array
, GQuark
, 0);
312 index
= (unsigned long) g_hash_table_lookup(variant_declaration
->untagged_variant
->fields_by_tag
,
313 (gconstpointer
) (unsigned long) tag
);
314 variant
->current_field
= g_ptr_array_index(variant
->fields
, index
);
315 return variant
->current_field
;