bt2: add option field class and field support
[babeltrace.git] / src / lib / trace-ir / field-class.h
CommitLineData
939190b3
PP
1#ifndef BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
2#define BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H
3
4/*
f2b0325d 5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
939190b3
PP
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
939190b3
PP
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
57952005 27#include "lib/assert-pre.h"
71c5da58
MJ
28#include <babeltrace2/trace-ir/clock-class.h>
29#include <babeltrace2/trace-ir/field-class.h>
85e7137b 30#include "common/macros.h"
02b61fe0 31#include "common/common.h"
57952005 32#include "lib/object.h"
71c5da58 33#include <babeltrace2/types.h>
939190b3
PP
34#include <stdint.h>
35#include <glib.h>
36
fa6cfec3
PP
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
02b61fe0
PP
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
fa6cfec3
PP
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)
71
72#define _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name) \
73 _name " is not an array field class: %![fc-]+F"
74
02b61fe0
PP
75#define _BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc) \
76 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR || \
77 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || \
78 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR)
79
80#define _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name) \
81 _name " is not a variant field class: %![fc-]+F"
82
83#define _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc) \
84 (((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || \
85 ((const struct bt_field_class *) (_fc))->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR)
86
87#define _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name) \
88 _name " is not a variant field class with a selector: %![fc-]+F"
89
fa6cfec3
PP
90#define _BT_ASSERT_PRE_FC_HAS_ID_COND(_fc, _type) \
91 (((const struct bt_field_class *) (_fc))->type == (_type))
92
93#define _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name) \
94 _name " has the wrong type: expected-type=%s, %![fc-]+F"
95
939190b3 96#define BT_ASSERT_PRE_FC_IS_INT(_fc, _name) \
fa6cfec3 97 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
02b61fe0 98 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
939190b3
PP
99
100#define BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(_fc, _name) \
fa6cfec3 101 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
02b61fe0
PP
102 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
103
104#define BT_ASSERT_PRE_FC_IS_SIGNED_INT(_fc, _name) \
105 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc), \
106 _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name), (_fc))
939190b3
PP
107
108#define BT_ASSERT_PRE_FC_IS_ENUM(_fc, _name) \
fa6cfec3 109 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
02b61fe0 110 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
939190b3
PP
111
112#define BT_ASSERT_PRE_FC_IS_ARRAY(_fc, _name) \
fa6cfec3 113 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
02b61fe0
PP
114 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
115
116#define BT_ASSERT_PRE_FC_IS_VARIANT(_fc, _name) \
117 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc), \
118 _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name), (_fc))
119
120#define BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL(_fc, _name) \
121 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc), \
122 _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name), (_fc))
939190b3 123
af0c18e3 124#define BT_ASSERT_PRE_FC_HAS_ID(_fc, _type, _name) \
fa6cfec3 125 BT_ASSERT_PRE(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
02b61fe0
PP
126 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
127 bt_common_field_class_type_string(_type), (_fc))
939190b3 128
fa6cfec3
PP
129#define BT_ASSERT_PRE_DEV_FC_IS_INT(_fc, _name) \
130 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_INT_COND(_fc), \
02b61fe0 131 _BT_ASSERT_PRE_FC_IS_INT_FMT(_name), (_fc))
fa6cfec3
PP
132
133#define BT_ASSERT_PRE_DEV_FC_IS_UNSIGNED_INT(_fc, _name) \
134 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_COND(_fc), \
02b61fe0
PP
135 _BT_ASSERT_PRE_FC_IS_UNSIGNED_INT_FMT(_name), (_fc))
136
137#define BT_ASSERT_PRE_DEV_FC_IS_SIGNED_INT(_fc, _name) \
138 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_SIGNED_INT_COND(_fc), \
139 _BT_ASSERT_PRE_FC_IS_SIGNED_INT_FMT(_name), (_fc))
fa6cfec3
PP
140
141#define BT_ASSERT_PRE_DEV_FC_IS_ENUM(_fc, _name) \
142 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ENUM_COND(_fc), \
02b61fe0 143 _BT_ASSERT_PRE_FC_IS_ENUM_FMT(_name), (_fc))
fa6cfec3
PP
144
145#define BT_ASSERT_PRE_DEV_FC_IS_ARRAY(_fc, _name) \
146 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_ARRAY_COND(_fc), \
02b61fe0
PP
147 _BT_ASSERT_PRE_FC_IS_ARRAY_FMT(_name), (_fc))
148
149#define BT_ASSERT_PRE_DEV_FC_IS_VARIANT(_fc, _name) \
150 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_VARIANT_COND(_fc), \
151 _BT_ASSERT_PRE_FC_IS_VARIANT_FMT(_name), (_fc))
152
153#define BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(_fc, _name) \
154 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_COND(_fc), \
155 _BT_ASSERT_PRE_FC_IS_VARIANT_WITH_SEL_FMT(_name), (_fc))
fa6cfec3
PP
156
157#define BT_ASSERT_PRE_DEV_FC_HAS_ID(_fc, _type, _name) \
158 BT_ASSERT_PRE_DEV(_BT_ASSERT_PRE_FC_HAS_ID_COND((_fc), (_type)), \
02b61fe0
PP
159 _BT_ASSERT_PRE_FC_HAS_ID_FMT(_name), \
160 bt_common_field_class_type_string(_type), (_fc))
fa6cfec3
PP
161
162#define BT_ASSERT_PRE_DEV_FC_HOT(_fc, _name) \
163 BT_ASSERT_PRE_DEV_HOT((const struct bt_field_class *) (_fc), \
939190b3
PP
164 (_name), ": %!+F", (_fc))
165
939190b3
PP
166#define BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(_fc, _index) \
167 (&g_array_index(((struct bt_field_class_enumeration *) (_fc))->mappings, \
168 struct bt_field_class_enumeration_mapping, (_index)))
169
170#define BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(_mapping, _index) \
171 (&g_array_index((_mapping)->ranges, \
172 struct bt_field_class_enumeration_mapping_range, (_index)))
173
174struct bt_field;
175struct bt_field_class;
176
177struct bt_field_class {
178 struct bt_object base;
af0c18e3 179 enum bt_field_class_type type;
939190b3
PP
180 bool frozen;
181
182 /*
fa6cfec3
PP
183 * This flag indicates whether or not this field class is part
184 * of a trace class.
939190b3 185 */
10b7a2e4 186 bool part_of_trace_class;
939190b3
PP
187};
188
9414b439
PP
189struct bt_field_class_bool {
190 struct bt_field_class common;
191};
192
939190b3
PP
193struct bt_field_class_integer {
194 struct bt_field_class common;
195
196 /*
197 * Value range of fields built from this integer field class:
198 * this is an equivalent integer size in bits. More formally,
199 * `range` is `n` in:
200 *
201 * Unsigned range: [0, 2^n - 1]
202 * Signed range: [-2^(n - 1), 2^(n - 1) - 1]
203 */
204 uint64_t range;
205
206 enum bt_field_class_integer_preferred_display_base base;
207};
208
939190b3
PP
209struct bt_field_class_enumeration_mapping {
210 GString *label;
211
02b61fe0
PP
212 /* Owner by this */
213 const struct bt_integer_range_set *range_set;
939190b3
PP
214};
215
60bbfc7c
PP
216struct bt_field_class_enumeration_unsigned_mapping;
217struct bt_field_class_enumeration_signed_mapping;
b6baf3bb 218
939190b3
PP
219struct bt_field_class_enumeration {
220 struct bt_field_class_integer common;
221
222 /* Array of `struct bt_field_class_enumeration_mapping *` */
223 GArray *mappings;
224
225 /*
226 * This is an array of `const char *` which acts as a temporary
227 * (potentially growing) buffer for
60bbfc7c 228 * bt_field_class_enumeration_unsigned_get_mapping_labels_for_value()
939190b3 229 * and
60bbfc7c 230 * bt_field_class_enumeration_signed_get_mapping_labels_for_value().
939190b3
PP
231 *
232 * The actual strings are owned by the mappings above.
233 */
234 GPtrArray *label_buf;
235};
236
237struct bt_field_class_real {
238 struct bt_field_class common;
239 bool is_single_precision;
240};
241
242struct bt_field_class_string {
243 struct bt_field_class common;
244};
245
246/* A named field class is a (name, field class) pair */
247struct bt_named_field_class {
248 GString *name;
249
250 /* Owned by this */
251 struct bt_field_class *fc;
e24f271a
PP
252
253 bool frozen;
939190b3
PP
254};
255
e24f271a
PP
256struct bt_field_class_structure_member;
257struct bt_field_class_variant_option;
60bbfc7c
PP
258struct bt_field_class_variant_with_selector_unsigned_option;
259struct bt_field_class_variant_with_selector_signed_option;
e24f271a 260
939190b3
PP
261struct bt_field_class_named_field_class_container {
262 struct bt_field_class common;
263
264 /*
265 * Key: `const char *`, not owned by this (owned by named field
02b61fe0 266 * class objects contained in `named_fcs` below).
939190b3
PP
267 */
268 GHashTable *name_to_index;
269
02b61fe0
PP
270 /* Array of `struct bt_named_field_class *` */
271 GPtrArray *named_fcs;
939190b3
PP
272};
273
274struct bt_field_class_structure {
275 struct bt_field_class_named_field_class_container common;
276};
277
278struct bt_field_class_array {
279 struct bt_field_class common;
280
281 /* Owned by this */
282 struct bt_field_class *element_fc;
283};
284
60bbfc7c 285struct bt_field_class_array_static {
939190b3
PP
286 struct bt_field_class_array common;
287 uint64_t length;
288};
289
60bbfc7c 290struct bt_field_class_array_dynamic {
939190b3
PP
291 struct bt_field_class_array common;
292
247fa9e3 293 /* Owned by this */
939190b3
PP
294 struct bt_field_class *length_fc;
295
296 /* Owned by this */
297 struct bt_field_path *length_field_path;
298};
299
247fa9e3
PP
300struct bt_field_class_option {
301 struct bt_field_class common;
302
303 /* Owned by this */
304 struct bt_field_class *content_fc;
305
306 /* Owned by this */
307 struct bt_field_class *selector_fc;
308
309 /* Owned by this */
310 struct bt_field_path *selector_field_path;
311};
312
02b61fe0
PP
313/* Variant FC (with selector) option: named field class + range set */
314struct bt_field_class_variant_with_selector_option {
315 struct bt_named_field_class common;
316
317 /* Owned by this */
318 const struct bt_integer_range_set *range_set;
319};
320
939190b3 321struct bt_field_class_variant {
02b61fe0
PP
322 /*
323 * Depending on the variant field class type, the contained
324 * named field classes are of type
325 * `struct bt_named_field_class *` if the variant field class
326 * doesn't have a selector, or
327 * `struct bt_field_class_variant_with_selector_option *`
328 * if it has.
329 */
939190b3 330 struct bt_field_class_named_field_class_container common;
02b61fe0 331};
939190b3 332
02b61fe0
PP
333struct bt_field_class_variant_with_selector {
334 struct bt_field_class_variant common;
335
336 /*
337 * Owned by this, but never dereferenced: only use to find it
338 * elsewhere.
339 */
340 const struct bt_field_class *selector_fc;
939190b3
PP
341
342 /* Owned by this */
343 struct bt_field_path *selector_field_path;
344};
345
939190b3 346BT_HIDDEN
78cf9df6 347void _bt_field_class_freeze(const struct bt_field_class *field_class);
939190b3
PP
348
349#ifdef BT_DEV_MODE
350# define bt_field_class_freeze _bt_field_class_freeze
351#else
931043f9 352# define bt_field_class_freeze(_fc) ((void) _fc)
939190b3
PP
353#endif
354
e24f271a
PP
355BT_HIDDEN
356void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc);
357
358#ifdef BT_DEV_MODE
359# define bt_named_field_class_freeze _bt_named_field_class_freeze
360#else
931043f9 361# define bt_named_field_class_freeze(_named_fc) ((void) _named_fc)
e24f271a
PP
362#endif
363
939190b3
PP
364/*
365 * This function recursively marks `field_class` and its children as
366 * being part of a trace. This is used to validate that all field classes
367 * are used at a single location within trace objects even if they are
368 * shared objects for other purposes.
369 */
370BT_HIDDEN
fa6cfec3 371void bt_field_class_make_part_of_trace_class(
10b7a2e4 372 const struct bt_field_class *field_class);
939190b3 373
939190b3 374#endif /* BABELTRACE_TRACE_IR_FIELD_CLASSES_INTERNAL_H */
This page took 0.07846 seconds and 4 git commands to generate.