lib: rename option/var. "selector" to "selector field"
[babeltrace.git] / src / lib / trace-ir / field-class.h
1 #ifndef BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
2 #define BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/clock-class.h>
29 #include <babeltrace2/trace-ir/field-class.h>
30 #include "common/macros.h"
31 #include "common/common.h"
32 #include "lib/object.h"
33 #include <babeltrace2/types.h>
34 #include <stdint.h>
35 #include <glib.h>
36
37 #define _BT_ASSERT_PRE_FC_IS_INT_COND(_fc) \
38 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
39 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
40 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
41 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
42
43 #define _BT_ASSERT_PRE_FC_IS_INT_FMT(_name) \
44 _name " is not an integer field class: %![fc-]+F"
45
46 #define _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc) \
47 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
48 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION)
49
50 #define _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name) \
51 _name " is not an unsigned integer field class: %![fc-]+F"
52
53
54 #define _BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc) \
55 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
56 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
57
58 #define _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name) \
59 _name " is not a signed integer field class: %![fc-]+F"
60
61 #define _BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc) \
62 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION || \
63 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION)
64
65 #define _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name) \
66 _name " is not an enumeration field class: %![fc-]+F"
67
68 #define _BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc) \
69 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
70 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \
71 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD)
72
73 #define _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name) \
74 _name " is not an array field class: %![fc-]+F"
75
76 #define _BT_ASSERT_PRE_FC_IS_OPTION_COND(_fc) \
77 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD || \
78 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \
79 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
80 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
81
82 #define _BT_ASSERT_PRE_FC_IS_OPTION_FMT(_name) \
83 _name " is not an option field class: %![fc-]+F"
84
85 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_COND(_fc) \
86 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \
87 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
88 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
89
90 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_FMT(_name) \
91 _name " is not an option field class with a selector: %![fc-]+F"
92
93 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_COND(_fc) \
94 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
95 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
96
97 #define _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_FMT(_name) \
98 _name " is not an option field class with an integer selector: %![fc-]+F"
99
100 #define _BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc) \
101 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD || \
102 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
103 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
104
105 #define _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name) \
106 _name " is not a variant field class: %![fc-]+F"
107
108 #define _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc) \
109 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
110 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD)
111
112 #define _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name) \
113 _name " is not a variant field class with a selector: %![fc-]+F"
114
115 #define _BT_ASSERT_PRE_FC_HAS_ID_COND(_fc, _type) \
116 (((const struct bt_field_class *) (_fc))->type == (_type))
117
118 #define _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name) \
119 _name " has the wrong type: expected-type=%s, %![fc-]+F"
120
121 #define BT_ASSERT_PRE_FC_IS_INT(_fc, _name) \
122 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
123 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
124
125 #define BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(_fc, _name) \
126 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
127 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
128
129 #define BT_ASSERT_PRE_FC_IS_SIGNED_INT(_fc, _name) \
130 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc), \
131 _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name), (_fc))
132
133 #define BT_ASSERT_PRE_FC_IS_ENUM(_fc, _name) \
134 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
135 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
136
137 #define BT_ASSERT_PRE_FC_IS_ARRAY(_fc, _name) \
138 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
139 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
140
141 #define BT_ASSERT_PRE_FC_IS_OPTION(_fc, _name) \
142 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_OPTION_COND(_fc), \
143 _BT_ASSERT_PRE_FC_IS_OPTION_FMT(_name), (_fc))
144
145 #define BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL(_fc, _name) \
146 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_COND(_fc), \
147 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_FMT(_name), (_fc))
148
149 #define BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(_fc, _name) \
150 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_COND(_fc), \
151 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_FMT(_name), (_fc))
152
153 #define BT_ASSERT_PRE_FC_IS_VARIANT(_fc, _name) \
154 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc), \
155 _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name), (_fc))
156
157 #define BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL(_fc, _name) \
158 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc), \
159 _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name), (_fc))
160
161 #define BT_ASSERT_PRE_FC_HAS_ID(_fc, _type, _name) \
162 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
163 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
164 bt_common_field_class_type_string(_type), (_fc))
165
166 #define BT_ASSERT_PRE_DEV_FC_IS_INT(_fc, _name) \
167 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
168 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
169
170 #define BT_ASSERT_PRE_DEV_FC_IS_UNSIGNED_INT(_fc, _name) \
171 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
172 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
173
174 #define BT_ASSERT_PRE_DEV_FC_IS_SIGNED_INT(_fc, _name) \
175 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc), \
176 _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name), (_fc))
177
178 #define BT_ASSERT_PRE_DEV_FC_IS_ENUM(_fc, _name) \
179 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
180 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
181
182 #define BT_ASSERT_PRE_DEV_FC_IS_ARRAY(_fc, _name) \
183 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
184 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
185
186 #define BT_ASSERT_PRE_DEV_FC_IS_OPTION(_fc, _name) \
187 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_OPTION_COND(_fc), \
188 _BT_ASSERT_PRE_FC_IS_OPTION_FMT(_name), (_fc))
189
190 #define BT_ASSERT_PRE_DEV_FC_IS_OPTION_WITH_SEL(_fc, _name) \
191 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_COND(_fc), \
192 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL_FMT(_name), (_fc))
193
194 #define BT_ASSERT_PRE_DEV_FC_IS_OPTION_WITH_INT_SEL(_fc, _name) \
195 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_COND(_fc), \
196 _BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL_FMT(_name), (_fc))
197
198 #define BT_ASSERT_PRE_DEV_FC_IS_VARIANT(_fc, _name) \
199 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc), \
200 _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name), (_fc))
201
202 #define BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(_fc, _name) \
203 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc), \
204 _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name), (_fc))
205
206 #define BT_ASSERT_PRE_DEV_FC_HAS_ID(_fc, _type, _name) \
207 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
208 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
209 bt_common_field_class_type_string(_type), (_fc))
210
211 #define BT_ASSERT_PRE_DEV_FC_HOT(_fc, _name) \
212 BT_ASSERT_PRE_DEV_HOT((const struct bt_field_class *) (_fc), \
213 (_name), ": %!+F", (_fc))
214
215 #define BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(_fc, _index) \
216 (&g_array_index(((struct bt_field_class_enumeration *) (_fc))->mappings, \
217 struct bt_field_class_enumeration_mapping, (_index)))
218
219 #define BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index) \
220 (&g_array_index((_mapping)->ranges, \
221 struct bt_field_class_enumeration_mapping_range, (_index)))
222
223 struct bt_field;
224 struct bt_field_class;
225
226 struct bt_field_class {
227 struct bt_object base;
228 enum bt_field_class_type type;
229 bool frozen;
230
231 /* Owned by this */
232 struct bt_value *user_attributes;
233
234 /*
235 * This flag indicates whether or not this field class is part
236 * of a trace class.
237 */
238 bool part_of_trace_class;
239 };
240
241 struct bt_field_class_bool {
242 struct bt_field_class common;
243 };
244
245 struct bt_field_class_bit_array {
246 struct bt_field_class common;
247 uint64_t length;
248 };
249
250 struct bt_field_class_integer {
251 struct bt_field_class common;
252
253 /*
254 * Value range of fields built from this integer field class:
255 * this is an equivalent integer size in bits. More formally,
256 * `range` is `n` in:
257 *
258 * Unsigned range: [0, 2^n - 1]
259 * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
260 */
261 uint64_t range;
262
263 enum bt_field_class_integer_preferred_display_base base;
264 };
265
266 struct bt_field_class_enumeration_mapping {
267 GString *label;
268
269 /* Owner by this */
270 const struct bt_integer_range_set *range_set;
271 };
272
273 struct bt_field_class_enumeration_unsigned_mapping;
274 struct bt_field_class_enumeration_signed_mapping;
275
276 struct bt_field_class_enumeration {
277 struct bt_field_class_integer common;
278
279 /* Array of `struct bt_field_class_enumeration_mapping *` */
280 GArray *mappings;
281
282 /*
283 * This is an array of `const char *` which acts as a temporary
284 * (potentially growing) buffer for
285 * bt_field_class_enumeration_unsigned_get_mapping_labels_for_value()
286 * and
287 * bt_field_class_enumeration_signed_get_mapping_labels_for_value().
288 *
289 * The actual strings are owned by the mappings above.
290 */
291 GPtrArray *label_buf;
292 };
293
294 struct bt_field_class_real {
295 struct bt_field_class common;
296 };
297
298 struct bt_field_class_string {
299 struct bt_field_class common;
300 };
301
302 /* A named field class is a (name, field class) pair */
303 struct bt_named_field_class {
304 GString *name;
305
306 /* Owned by this */
307 struct bt_value *user_attributes;
308
309 /* Owned by this */
310 struct bt_field_class *fc;
311
312 bool frozen;
313 };
314
315 struct bt_field_class_structure_member;
316 struct bt_field_class_variant_option;
317 struct bt_field_class_variant_with_selector_field_integer_unsigned_option;
318 struct bt_field_class_variant_with_selector_field_integer_signed_option;
319
320 struct bt_field_class_named_field_class_container {
321 struct bt_field_class common;
322
323 /*
324 * Key: `const char *`, not owned by this (owned by named field
325 * class objects contained in `named_fcs` below).
326 */
327 GHashTable *name_to_index;
328
329 /* Array of `struct bt_named_field_class *` */
330 GPtrArray *named_fcs;
331 };
332
333 struct bt_field_class_structure {
334 struct bt_field_class_named_field_class_container common;
335 };
336
337 struct bt_field_class_array {
338 struct bt_field_class common;
339
340 /* Owned by this */
341 struct bt_field_class *element_fc;
342 };
343
344 struct bt_field_class_array_static {
345 struct bt_field_class_array common;
346 uint64_t length;
347 };
348
349 struct bt_field_class_array_dynamic {
350 struct bt_field_class_array common;
351
352 /* Owned by this */
353 struct bt_field_class *length_fc;
354
355 /* Owned by this */
356 struct bt_field_path *length_field_path;
357 };
358
359 struct bt_field_class_option {
360 struct bt_field_class common;
361
362 /* Owned by this */
363 struct bt_field_class *content_fc;
364 };
365
366 struct bt_field_class_option_with_selector_field {
367 struct bt_field_class_option common;
368
369 /* Owned by this */
370 struct bt_field_class *selector_fc;
371
372 /* Owned by this */
373 struct bt_field_path *selector_field_path;
374 };
375
376 struct bt_field_class_option_with_selector_field_bool {
377 struct bt_field_class_option_with_selector_field common;
378
379 /* Owned by this */
380 bool sel_is_reversed;
381 };
382
383 struct bt_field_class_option_with_selector_field_integer {
384 struct bt_field_class_option_with_selector_field common;
385
386 /* Owned by this */
387 const struct bt_integer_range_set *range_set;
388 };
389
390 /* Variant FC (with selector) option: named field class + range set */
391 struct bt_field_class_variant_with_selector_field_option {
392 struct bt_named_field_class common;
393
394 /* Owned by this */
395 const struct bt_integer_range_set *range_set;
396 };
397
398 struct bt_field_class_variant {
399 /*
400 * Depending on the variant field class type, the contained
401 * named field classes are of type
402 * `struct bt_named_field_class *` if the variant field class
403 * doesn't have a selector, or
404 * `struct bt_field_class_variant_with_selector_field_option *`
405 * if it has.
406 */
407 struct bt_field_class_named_field_class_container common;
408 };
409
410 struct bt_field_class_variant_with_selector_field {
411 struct bt_field_class_variant common;
412
413 /*
414 * Owned by this, but never dereferenced: only use to find it
415 * elsewhere.
416 */
417 const struct bt_field_class *selector_fc;
418
419 /* Owned by this */
420 struct bt_field_path *selector_field_path;
421 };
422
423 BT_HIDDEN
424 void _bt_field_class_freeze(const struct bt_field_class *field_class);
425
426 #ifdef BT_DEV_MODE
427 # define bt_field_class_freeze _bt_field_class_freeze
428 #else
429 # define bt_field_class_freeze(_fc) ((void) _fc)
430 #endif
431
432 BT_HIDDEN
433 void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc);
434
435 #ifdef BT_DEV_MODE
436 # define bt_named_field_class_freeze _bt_named_field_class_freeze
437 #else
438 # define bt_named_field_class_freeze(_named_fc) ((void) _named_fc)
439 #endif
440
441 /*
442 * This function recursively marks `field_class` and its children as
443 * being part of a trace. This is used to validate that all field classes
444 * are used at a single location within trace objects even if they are
445 * shared objects for other purposes.
446 */
447 BT_HIDDEN
448 void bt_field_class_make_part_of_trace_class(
449 const struct bt_field_class *field_class);
450
451 #endif /* BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H */
This page took 0.039847 seconds and 4 git commands to generate.