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 int variant_rw(struct stream_pos
*ppos
, struct definition
*definition
)
32 struct definition_variant
*variant_definition
=
33 container_of(definition
, struct definition_variant
, p
);
36 field
= variant_get_current_field(variant_definition
);
37 return generic_rw(ppos
, field
->definition
);
41 void _untagged_variant_declaration_free(struct declaration
*declaration
)
43 struct declaration_untagged_variant
*untagged_variant_declaration
=
44 container_of(declaration
, struct declaration_untagged_variant
, p
);
47 free_declaration_scope(untagged_variant_declaration
->scope
);
48 g_hash_table_destroy(untagged_variant_declaration
->fields_by_tag
);
50 for (i
= 0; i
< untagged_variant_declaration
->fields
->len
; i
++) {
51 struct declaration_field
*declaration_field
=
52 &g_array_index(untagged_variant_declaration
->fields
,
53 struct declaration_field
, i
);
54 declaration_unref(declaration_field
->declaration
);
56 g_array_free(untagged_variant_declaration
->fields
, true);
57 g_free(untagged_variant_declaration
);
60 struct declaration_untagged_variant
*untagged_variant_declaration_new(
61 struct declaration_scope
*parent_scope
)
63 struct declaration_untagged_variant
*untagged_variant_declaration
;
64 struct declaration
*declaration
;
66 untagged_variant_declaration
= g_new(struct declaration_untagged_variant
, 1);
67 declaration
= &untagged_variant_declaration
->p
;
68 untagged_variant_declaration
->fields_by_tag
= g_hash_table_new(g_direct_hash
,
70 untagged_variant_declaration
->fields
= g_array_sized_new(FALSE
, TRUE
,
71 sizeof(struct declaration_field
),
72 DEFAULT_NR_STRUCT_FIELDS
);
73 untagged_variant_declaration
->scope
= new_declaration_scope(parent_scope
);
74 declaration
->id
= CTF_TYPE_UNTAGGED_VARIANT
;
75 declaration
->alignment
= 1;
76 declaration
->declaration_free
= _untagged_variant_declaration_free
;
77 declaration
->definition_new
= NULL
;
78 declaration
->definition_free
= NULL
;
80 return untagged_variant_declaration
;
84 void _variant_declaration_free(struct declaration
*declaration
)
86 struct declaration_variant
*variant_declaration
=
87 container_of(declaration
, struct declaration_variant
, p
);
89 _untagged_variant_declaration_free(&variant_declaration
->untagged_variant
->p
);
90 g_array_free(variant_declaration
->tag_name
, TRUE
);
93 struct declaration_variant
*
94 variant_declaration_new(struct declaration_untagged_variant
*untagged_variant
, const char *tag
)
96 struct declaration_variant
*variant_declaration
;
97 struct declaration
*declaration
;
99 variant_declaration
= g_new(struct declaration_variant
, 1);
100 declaration
= &variant_declaration
->p
;
101 variant_declaration
->untagged_variant
= untagged_variant
;
102 variant_declaration
->tag_name
= g_array_new(FALSE
, TRUE
, sizeof(GQuark
));
103 append_scope_path(tag
, variant_declaration
->tag_name
);
104 declaration
->id
= CTF_TYPE_VARIANT
;
105 declaration
->alignment
= 1;
106 declaration
->declaration_free
= _variant_declaration_free
;
107 declaration
->definition_new
= _variant_definition_new
;
108 declaration
->definition_free
= _variant_definition_free
;
109 declaration
->ref
= 1;
110 return variant_declaration
;
114 * tag_instance is assumed to be an enumeration.
115 * Returns 0 if OK, < 0 if error.
118 int check_enum_tag(struct definition_variant
*variant
,
119 struct definition
*enum_tag
)
121 struct definition_enum
*_enum
=
122 container_of(enum_tag
, struct definition_enum
, p
);
123 struct declaration_enum
*enum_declaration
= _enum
->declaration
;
124 int missing_field
= 0;
128 * Strictly speaking, each enumerator must map to a field of the
129 * variant. However, we are even stricter here by requiring that each
130 * variant choice map to an enumerator too. We then validate that the
131 * number of enumerators equals the number of variant choices.
133 if (variant
->declaration
->untagged_variant
->fields
->len
!= enum_get_nr_enumerators(enum_declaration
))
136 for (i
= 0; i
< variant
->declaration
->untagged_variant
->fields
->len
; i
++) {
137 struct declaration_field
*field_declaration
=
138 &g_array_index(variant
->declaration
->untagged_variant
->fields
,
139 struct declaration_field
, i
);
140 if (!enum_quark_to_range_set(enum_declaration
, field_declaration
->name
)) {
149 * Check the enumeration: it must map each value to one and only one
151 * TODO: we should also check that each range map to one and only one
152 * tag. For the moment, we will simply check this dynamically in
153 * variant_declaration_get_current_field().
162 _variant_definition_new(struct declaration
*declaration
,
163 struct definition_scope
*parent_scope
,
164 GQuark field_name
, int index
)
166 struct declaration_variant
*variant_declaration
=
167 container_of(declaration
, struct declaration_variant
, p
);
168 struct definition_variant
*variant
;
171 variant
= g_new(struct definition_variant
, 1);
172 declaration_ref(&variant_declaration
->p
);
173 variant
->p
.declaration
= declaration
;
174 variant
->declaration
= variant_declaration
;
176 variant
->p
.index
= index
;
177 variant
->scope
= new_definition_scope(parent_scope
, field_name
);
178 variant
->enum_tag
= lookup_definition(variant
->scope
->scope_path
,
179 variant_declaration
->tag_name
,
182 if (!variant
->enum_tag
183 || check_enum_tag(variant
, variant
->enum_tag
) < 0)
185 definition_ref(variant
->enum_tag
);
186 variant
->fields
= g_array_sized_new(FALSE
, TRUE
,
187 sizeof(struct field
),
188 variant_declaration
->untagged_variant
->fields
->len
);
189 g_array_set_size(variant
->fields
, variant_declaration
->untagged_variant
->fields
->len
);
190 for (i
= 0; i
< variant_declaration
->untagged_variant
->fields
->len
; i
++) {
191 struct declaration_field
*declaration_field
=
192 &g_array_index(variant_declaration
->untagged_variant
->fields
,
193 struct declaration_field
, i
);
194 struct field
*field
= &g_array_index(variant
->fields
,
197 field
->name
= declaration_field
->name
;
199 * All child definition are at index 0, because they are
200 * various choices of the same field.
203 declaration_field
->declaration
->definition_new(declaration_field
->declaration
,
207 variant
->current_field
= NULL
;
210 free_definition_scope(variant
->scope
);
211 declaration_unref(&variant_declaration
->p
);
217 void _variant_definition_free(struct definition
*definition
)
219 struct definition_variant
*variant
=
220 container_of(definition
, struct definition_variant
, p
);
223 assert(variant
->fields
->len
== variant
->declaration
->untagged_variant
->fields
->len
);
224 for (i
= 0; i
< variant
->fields
->len
; i
++) {
225 struct field
*field
= &g_array_index(variant
->fields
,
227 definition_unref(field
->definition
);
229 definition_unref(variant
->enum_tag
);
230 free_definition_scope(variant
->scope
);
231 declaration_unref(variant
->p
.declaration
);
235 void untagged_variant_declaration_add_field(struct declaration_untagged_variant
*untagged_variant_declaration
,
236 const char *field_name
,
237 struct declaration
*field_declaration
)
239 struct declaration_field
*field
;
242 g_array_set_size(untagged_variant_declaration
->fields
, untagged_variant_declaration
->fields
->len
+ 1);
243 index
= untagged_variant_declaration
->fields
->len
- 1; /* last field (new) */
244 field
= &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
245 field
->name
= g_quark_from_string(field_name
);
246 declaration_ref(field_declaration
);
247 field
->declaration
= field_declaration
;
248 /* Keep index in hash rather than pointer, because array can relocate */
249 g_hash_table_insert(untagged_variant_declaration
->fields_by_tag
,
250 (gpointer
) (unsigned long) field
->name
,
253 * Alignment of variant is based on the alignment of its currently
254 * selected choice, so we leave variant alignment as-is (statically
259 struct declaration_field
*
260 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant
*untagged_variant_declaration
, GQuark tag
)
264 index
= (unsigned long) g_hash_table_lookup(untagged_variant_declaration
->fields_by_tag
,
265 (gconstpointer
) (unsigned long) tag
);
266 return &g_array_index(untagged_variant_declaration
->fields
, struct declaration_field
, index
);
270 * field returned only valid as long as the field structure is not appended to.
272 struct field
*variant_get_current_field(struct definition_variant
*variant
)
274 struct definition_enum
*_enum
=
275 container_of(variant
->enum_tag
, struct definition_enum
, p
);
276 struct declaration_variant
*variant_declaration
= variant
->declaration
;
281 tag_array
= _enum
->value
;
283 * The 1 to 1 mapping from enumeration to value should have been already
284 * checked. (see TODO above)
286 assert(tag_array
->len
== 1);
287 tag
= g_array_index(tag_array
, GQuark
, 0);
288 index
= (unsigned long) g_hash_table_lookup(variant_declaration
->untagged_variant
->fields_by_tag
,
289 (gconstpointer
) (unsigned long) tag
);
290 variant
->current_field
= &g_array_index(variant
->fields
, struct field
, index
);
291 return variant
->current_field
;