Fix: allow duplicate keys and overlapping ranges in enumerations
[babeltrace.git] / include / babeltrace / ctf-ir / field-types-internal.h
1 #ifndef BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
3
4 /*
5 * BabelTrace - CTF IR: Event field types internal
6 *
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/ctf-writer/event-types.h>
31 #include <babeltrace/ctf-writer/event-fields.h>
32 #include <babeltrace/ctf-writer/writer.h>
33 #include <babeltrace/ctf-ir/trace-internal.h>
34 #include <babeltrace/ctf-ir/clock-class.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/object-internal.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/ctf/events.h>
39 #include <glib.h>
40
41 typedef void (*type_freeze_func)(struct bt_ctf_field_type *);
42 typedef int (*type_serialize_func)(struct bt_ctf_field_type *,
43 struct metadata_context *);
44
45 struct bt_ctf_field_type {
46 struct bt_object base;
47 struct bt_declaration *declaration;
48 type_freeze_func freeze;
49 type_serialize_func serialize;
50 /*
51 * A type can't be modified once it is added to an event or after a
52 * a field has been instanciated from it.
53 */
54 int frozen;
55
56 /*
57 * This flag indicates if the field type is valid. A valid
58 * field type is _always_ frozen. All the nested field types of
59 * a valid field type are also valid (and thus frozen).
60 */
61 int valid;
62 };
63
64 struct bt_ctf_field_type_integer {
65 struct bt_ctf_field_type parent;
66 struct declaration_integer declaration;
67 struct bt_ctf_clock_class *mapped_clock;
68
69 /*
70 * This is what the user sets and is never modified by internal
71 * code.
72 *
73 * This field must contain a `BT_CTF_BYTE_ORDER_*` value.
74 */
75 enum bt_ctf_byte_order user_byte_order;
76 };
77
78 struct enumeration_mapping {
79 union {
80 uint64_t _unsigned;
81 int64_t _signed;
82 } range_start;
83
84 union {
85 uint64_t _unsigned;
86 int64_t _signed;
87 } range_end;
88 GQuark string;
89 };
90
91 struct bt_ctf_field_type_enumeration {
92 struct bt_ctf_field_type parent;
93 struct bt_ctf_field_type *container;
94 GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */
95 struct declaration_enum declaration;
96 bool has_overlapping_ranges;
97 };
98
99 enum bt_ctf_field_type_enumeration_mapping_iterator_kind {
100 ITERATOR_BY_NAME,
101 ITERATOR_BY_SIGNED_VALUE,
102 ITERATOR_BY_UNSIGNED_VALUE,
103 };
104
105 struct bt_ctf_field_type_enumeration_mapping_iterator {
106 struct bt_object base;
107 struct bt_ctf_field_type_enumeration *enumeration_type;
108 enum bt_ctf_field_type_enumeration_mapping_iterator_kind kind;
109 int index;
110 union {
111 GQuark name_quark;
112 int64_t signed_value;
113 uint64_t unsigned_value;
114 } u;
115 };
116
117 struct bt_ctf_field_type_floating_point {
118 struct bt_ctf_field_type parent;
119 struct declaration_float declaration;
120
121 /*
122 * The `declaration` field above contains 3 pointers pointing
123 * to the fields below. This avoids unnecessary dynamic
124 * allocations.
125 */
126 struct declaration_integer sign;
127 struct declaration_integer mantissa;
128 struct declaration_integer exp;
129
130 /*
131 * This is what the user sets and is never modified by internal
132 * code.
133 *
134 * This field must contain a `BT_CTF_BYTE_ORDER_*` value.
135 */
136 enum bt_ctf_byte_order user_byte_order;
137 };
138
139 struct structure_field {
140 GQuark name;
141 struct bt_ctf_field_type *type;
142 };
143
144 struct bt_ctf_field_type_structure {
145 struct bt_ctf_field_type parent;
146 GHashTable *field_name_to_index;
147 GPtrArray *fields; /* Array of pointers to struct structure_field */
148 struct declaration_struct declaration;
149 };
150
151 struct bt_ctf_field_type_variant {
152 struct bt_ctf_field_type parent;
153 GString *tag_name;
154 struct bt_ctf_field_type_enumeration *tag;
155 struct bt_ctf_field_path *tag_field_path;
156 GHashTable *field_name_to_index;
157 GPtrArray *fields; /* Array of pointers to struct structure_field */
158 struct declaration_variant declaration;
159 };
160
161 struct bt_ctf_field_type_array {
162 struct bt_ctf_field_type parent;
163 struct bt_ctf_field_type *element_type;
164 unsigned int length; /* Number of elements */
165 struct declaration_array declaration;
166 };
167
168 struct bt_ctf_field_type_sequence {
169 struct bt_ctf_field_type parent;
170 struct bt_ctf_field_type *element_type;
171 GString *length_field_name;
172 struct bt_ctf_field_path *length_field_path;
173 struct declaration_sequence declaration;
174 };
175
176 struct bt_ctf_field_type_string {
177 struct bt_ctf_field_type parent;
178 struct declaration_string declaration;
179 };
180
181 BT_HIDDEN
182 void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type);
183
184 BT_HIDDEN
185 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
186 struct bt_ctf_field_type_variant *variant, int64_t tag_value);
187
188 BT_HIDDEN
189 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
190 struct bt_ctf_field_type_variant *variant, uint64_t tag_value);
191
192 BT_HIDDEN
193 int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
194 struct metadata_context *context);
195
196 BT_HIDDEN
197 int bt_ctf_field_type_validate(struct bt_ctf_field_type *type);
198
199 /* Override field type's byte order only if it is set to "native" */
200 BT_HIDDEN
201 void bt_ctf_field_type_set_native_byte_order(
202 struct bt_ctf_field_type *type, int byte_order);
203
204 BT_HIDDEN
205 int bt_ctf_field_type_structure_get_field_name_index(
206 struct bt_ctf_field_type *structure, const char *name);
207
208 /* Replace an existing field's type in a structure */
209 BT_HIDDEN
210 int bt_ctf_field_type_structure_set_field_index(
211 struct bt_ctf_field_type *structure,
212 struct bt_ctf_field_type *field, int index);
213
214 BT_HIDDEN
215 int bt_ctf_field_type_variant_get_field_name_index(
216 struct bt_ctf_field_type *variant, const char *name);
217
218 BT_HIDDEN
219 int bt_ctf_field_type_sequence_set_length_field_path(
220 struct bt_ctf_field_type *type,
221 struct bt_ctf_field_path *path);
222
223 BT_HIDDEN
224 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
225 struct bt_ctf_field_path *path);
226
227 BT_HIDDEN
228 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
229 struct bt_ctf_field_type *tag_type);
230
231 /* Replace an existing field's type in a variant */
232 BT_HIDDEN
233 int bt_ctf_field_type_variant_set_field_index(
234 struct bt_ctf_field_type *variant,
235 struct bt_ctf_field_type *field, int index);
236
237 BT_HIDDEN
238 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array,
239 struct bt_ctf_field_type *element_type);
240
241 BT_HIDDEN
242 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array,
243 struct bt_ctf_field_type *element_type);
244
245 BT_HIDDEN
246 int bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type);
247
248 BT_HIDDEN
249 struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
250 struct bt_ctf_field_type *type, int index);
251
252 BT_HIDDEN
253 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type,
254 const char *name);
255
256 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */
This page took 0.039904 seconds and 5 git commands to generate.