lib: merge common CTF IR part with the remaining implementation
[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/assert-pre-internal.h>
31 #include <babeltrace/ctf-ir/clock-class.h>
32 #include <babeltrace/ctf-ir/field-types.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/object-internal.h>
35 #include <babeltrace/types.h>
36 #include <stdint.h>
37 #include <glib.h>
38
39 #define BT_ASSERT_PRE_FT_HAS_ID(_ft, _type_id, _name) \
40 BT_ASSERT_PRE(((struct bt_field_type *) (_ft))->id == (_type_id), \
41 _name " has the wrong type ID: expected-type-id=%s, " \
42 "%![ft-]+F", bt_common_field_type_id_string(_type_id), (_ft))
43
44 #define BT_ASSERT_PRE_FT_HOT(_ft, _name) \
45 BT_ASSERT_PRE_HOT((_ft), (_name), ": %!+F", (_ft))
46
47 #define BT_FIELD_TYPE_STRUCTURE_FIELD_AT_INDEX(_ft, _index) \
48 (&g_array_index(((struct bt_field_type_structure *) (_ft))->fields, \
49 struct bt_field_type_structure_field, (_index)))
50
51 #define BT_FIELD_TYPE_VARIANT_CHOICE_AT_INDEX(_ft, _index) \
52 (&g_array_index(((struct bt_field_type_variant *) (_ft))->choices, \
53 struct bt_field_type_variant_choice, (_index)))
54
55 struct bt_field;
56 struct bt_field_type;
57
58 typedef void (*bt_field_type_method_freeze)(
59 struct bt_field_type *);
60 typedef int (*bt_field_type_method_validate)(
61 struct bt_field_type *);
62 typedef void (*bt_field_type_method_set_byte_order)(
63 struct bt_field_type *, enum bt_byte_order);
64 typedef struct bt_field_type *(*bt_field_type_method_copy)(
65 struct bt_field_type *);
66 typedef int (*bt_field_type_method_compare)(
67 struct bt_field_type *,
68 struct bt_field_type *);
69
70 struct bt_field_type_methods {
71 bt_field_type_method_freeze freeze;
72 bt_field_type_method_validate validate;
73 bt_field_type_method_set_byte_order set_byte_order;
74 bt_field_type_method_copy copy;
75 bt_field_type_method_compare compare;
76 };
77
78 struct bt_field_type {
79 struct bt_object base;
80 enum bt_field_type_id id;
81 unsigned int alignment;
82
83 /* Virtual table */
84 struct bt_field_type_methods *methods;
85
86 /*
87 * A type can't be modified once it is added to an event or after a
88 * a field has been instanciated from it.
89 */
90 int frozen;
91
92 /*
93 * This flag indicates if the field type is valid. A valid
94 * field type is _always_ frozen. All the nested field types of
95 * a valid field type are also valid (and thus frozen).
96 */
97 int valid;
98 };
99
100 struct bt_field_type_integer {
101 struct bt_field_type common;
102
103 /* Owned by this */
104 struct bt_clock_class *mapped_clock_class;
105
106 enum bt_byte_order user_byte_order;
107 bt_bool is_signed;
108 unsigned int size;
109 enum bt_integer_base base;
110 enum bt_string_encoding encoding;
111 };
112
113 struct enumeration_mapping {
114 union {
115 uint64_t _unsigned;
116 int64_t _signed;
117 } range_start;
118 union {
119 uint64_t _unsigned;
120 int64_t _signed;
121 } range_end;
122 GQuark string;
123 };
124
125 struct bt_field_type_enumeration {
126 struct bt_field_type common;
127
128 /* Owned by this */
129 struct bt_field_type_integer *container_ft;
130
131 /* Array of `struct enumeration_mapping *`, owned by this */
132 GPtrArray *entries;
133
134 /* Only set during validation */
135 bt_bool has_overlapping_ranges;
136 };
137
138 enum bt_field_type_enumeration_mapping_iterator_type {
139 ITERATOR_BY_NAME,
140 ITERATOR_BY_SIGNED_VALUE,
141 ITERATOR_BY_UNSIGNED_VALUE,
142 };
143
144 struct bt_field_type_enumeration_mapping_iterator {
145 struct bt_object base;
146
147 /* Owned by this */
148 struct bt_field_type_enumeration *enumeration_ft;
149
150 enum bt_field_type_enumeration_mapping_iterator_type type;
151 int index;
152 union {
153 GQuark name_quark;
154 int64_t signed_value;
155 uint64_t unsigned_value;
156 } u;
157 };
158
159 struct bt_field_type_floating_point {
160 struct bt_field_type common;
161 enum bt_byte_order user_byte_order;
162 unsigned int exp_dig;
163 unsigned int mant_dig;
164 };
165
166 struct bt_field_type_structure_field {
167 GQuark name;
168
169 /* Owned by this */
170 struct bt_field_type *type;
171 };
172
173 struct bt_field_type_structure {
174 struct bt_field_type common;
175 GHashTable *field_name_to_index;
176
177 /*
178 * Array of `struct bt_field_type_structure_field`,
179 * owned by this
180 */
181 GArray *fields;
182 };
183
184 struct bt_field_type_variant_choice_range {
185 union {
186 int64_t i;
187 uint64_t u;
188 } lower;
189 union {
190 int64_t i;
191 uint64_t u;
192 } upper;
193 };
194
195 struct bt_field_type_variant_choice {
196 GQuark name;
197
198 /* Owned by this */
199 struct bt_field_type *type;
200
201 /* Array of `struct bt_field_type_variant_choice_range` */
202 GArray *ranges;
203 };
204
205 struct bt_field_type_variant {
206 struct bt_field_type common;
207 GString *tag_name;
208 bool choices_up_to_date;
209
210 /* Owned by this */
211 struct bt_field_type_enumeration *tag_ft;
212
213 /* Owned by this */
214 struct bt_field_path *tag_field_path;
215
216 GHashTable *choice_name_to_index;
217
218 /*
219 * Array of `struct bt_field_type_variant_choice`,
220 * owned by this */
221 GArray *choices;
222 };
223
224 struct bt_field_type_array {
225 struct bt_field_type common;
226
227 /* Owned by this */
228 struct bt_field_type *element_ft;
229
230 unsigned int length;
231 };
232
233 struct bt_field_type_sequence {
234 struct bt_field_type common;
235
236 /* Owned by this */
237 struct bt_field_type *element_ft;
238
239 GString *length_field_name;
240
241 /* Owned by this */
242 struct bt_field_path *length_field_path;
243 };
244
245 struct bt_field_type_string {
246 struct bt_field_type common;
247 enum bt_string_encoding encoding;
248 };
249
250 typedef struct bt_field *(* bt_field_create_func)(
251 struct bt_field_type *);
252
253 BT_ASSERT_FUNC
254 static inline bool bt_field_type_has_known_id(
255 struct bt_field_type *ft)
256 {
257 return (int) ft->id > BT_FIELD_TYPE_ID_UNKNOWN ||
258 (int) ft->id < BT_FIELD_TYPE_ID_NR;
259 }
260
261 BT_HIDDEN
262 int bt_field_type_variant_update_choices(
263 struct bt_field_type *ft);
264
265 BT_HIDDEN
266 void bt_field_type_freeze(struct bt_field_type *ft);
267
268 BT_HIDDEN
269 int bt_field_type_validate(struct bt_field_type *ft);
270
271 BT_HIDDEN
272 int bt_field_type_sequence_set_length_field_path(
273 struct bt_field_type *ft, struct bt_field_path *path);
274
275 BT_HIDDEN
276 int bt_field_type_variant_set_tag_field_path(
277 struct bt_field_type *ft,
278 struct bt_field_path *path);
279
280 BT_HIDDEN
281 int bt_field_type_variant_set_tag_field_type(
282 struct bt_field_type *ft,
283 struct bt_field_type *tag_ft);
284
285 BT_HIDDEN
286 int64_t bt_field_type_get_field_count(struct bt_field_type *ft);
287
288 BT_HIDDEN
289 struct bt_field_type *bt_field_type_borrow_field_at_index(
290 struct bt_field_type *ft, int index);
291
292 BT_HIDDEN
293 int bt_field_type_get_field_index(struct bt_field_type *ft,
294 const char *name);
295
296 BT_HIDDEN
297 int bt_field_type_validate_single_clock_class(
298 struct bt_field_type *ft,
299 struct bt_clock_class **expected_clock_class);
300
301 BT_HIDDEN
302 int64_t bt_field_type_variant_find_choice_index(
303 struct bt_field_type *ft, uint64_t uval,
304 bool is_signed);
305
306 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */
This page took 0.0357 seconds and 4 git commands to generate.