Commit | Line | Data |
---|---|---|
0220be14 JG |
1 | /* |
2 | * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include "field.hpp" | |
9 | ||
10 | #include <common/exception.hpp> | |
11 | #include <common/format.hpp> | |
12 | ||
d7bfb9b0 JG |
13 | #include <set> |
14 | ||
0220be14 JG |
15 | namespace lst = lttng::sessiond::trace; |
16 | ||
17 | namespace { | |
18 | template <class FieldTypeSet> | |
19 | bool fields_are_equal(const FieldTypeSet& a, const FieldTypeSet& b) | |
20 | { | |
21 | if (a.size() != b.size()) { | |
22 | return false; | |
23 | } | |
24 | ||
28ab034a JG |
25 | return std::equal(a.cbegin(), |
26 | a.cend(), | |
27 | b.cbegin(), | |
28 | [](typename FieldTypeSet::const_reference field_a, | |
29 | typename FieldTypeSet::const_reference field_b) { | |
30 | return *field_a == *field_b; | |
31 | }); | |
0220be14 JG |
32 | } |
33 | } /* namespace */ | |
34 | ||
eda1aa02 | 35 | lst::field_location::field_location(lst::field_location::root in_lookup_root, |
28ab034a JG |
36 | lst::field_location::elements in_elements) : |
37 | root_{ in_lookup_root }, elements_{ std::move(in_elements) } | |
eda1aa02 JG |
38 | { |
39 | } | |
40 | ||
41 | bool lst::field_location::operator==(const lst::field_location& other) const noexcept | |
42 | { | |
28ab034a | 43 | return root_ == other.root_ && elements_ == other.elements_; |
eda1aa02 JG |
44 | } |
45 | ||
28ab034a | 46 | lst::type::type(unsigned int in_alignment) : alignment{ in_alignment } |
0220be14 JG |
47 | { |
48 | } | |
49 | ||
50 | lst::type::~type() | |
51 | { | |
52 | } | |
53 | ||
54 | bool lst::type::operator==(const lst::type& other) const noexcept | |
55 | { | |
28ab034a | 56 | return typeid(*this) == typeid(other) && alignment == other.alignment && |
0220be14 JG |
57 | /* defer to concrete type comparison */ |
58 | this->_is_equal(other); | |
59 | } | |
60 | ||
61 | bool lst::type::operator!=(const lst::type& other) const noexcept | |
62 | { | |
63 | return !(*this == other); | |
64 | } | |
65 | ||
66 | lst::field::field(std::string in_name, lst::type::cuptr in_type) : | |
28ab034a | 67 | name{ std::move(in_name) }, _type{ std::move(in_type) } |
0220be14 | 68 | { |
45110cdd | 69 | if (!_type) { |
28ab034a JG |
70 | LTTNG_THROW_ERROR( |
71 | fmt::format("Invalid type used to create field: field name = `{}`", name)); | |
45110cdd | 72 | } |
0220be14 JG |
73 | } |
74 | ||
75 | void lst::field::accept(lst::field_visitor& visitor) const | |
76 | { | |
77 | visitor.visit(*this); | |
78 | } | |
79 | ||
80 | bool lst::field::operator==(const lst::field& other) const noexcept | |
81 | { | |
82 | return name == other.name && *_type == *other._type; | |
83 | } | |
84 | ||
45110cdd JG |
85 | lst::type::cuptr lst::field::move_type() noexcept |
86 | { | |
87 | return std::move(_type); | |
88 | } | |
89 | ||
28ab034a | 90 | const lst::type& lst::field::get_type() const |
45110cdd JG |
91 | { |
92 | if (_type) { | |
93 | return *_type; | |
94 | } else { | |
28ab034a JG |
95 | LTTNG_THROW_ERROR(fmt::format( |
96 | "Invalid attempt to access field type after transfer: field name = `{}`", | |
97 | name)); | |
45110cdd JG |
98 | } |
99 | } | |
100 | ||
0220be14 | 101 | lst::integer_type::integer_type(unsigned int in_alignment, |
28ab034a JG |
102 | enum lst::byte_order in_byte_order, |
103 | unsigned int in_size, | |
104 | enum lst::integer_type::signedness in_signedness, | |
105 | enum lst::integer_type::base in_base, | |
106 | roles in_roles) : | |
0220be14 | 107 | type(in_alignment), |
28ab034a JG |
108 | byte_order{ in_byte_order }, |
109 | size{ in_size }, | |
110 | signedness_{ in_signedness }, | |
111 | base_{ in_base }, | |
112 | roles_{ std::move(in_roles) } | |
0220be14 JG |
113 | { |
114 | } | |
115 | ||
b6bbb1d6 JG |
116 | lst::type::cuptr lst::integer_type::copy() const |
117 | { | |
118 | return lttng::make_unique<integer_type>( | |
28ab034a | 119 | alignment, byte_order, size, signedness_, base_, roles_); |
b6bbb1d6 JG |
120 | } |
121 | ||
28ab034a | 122 | bool lst::integer_type::_is_equal(const type& base_other) const noexcept |
0220be14 JG |
123 | { |
124 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
125 | ||
28ab034a JG |
126 | return this->byte_order == other.byte_order && this->size == other.size && |
127 | this->signedness_ == other.signedness_ && this->base_ == other.base_ && | |
e7360180 | 128 | this->roles_ == other.roles_; |
0220be14 JG |
129 | } |
130 | ||
131 | void lst::integer_type::accept(type_visitor& visitor) const | |
132 | { | |
133 | visitor.visit(*this); | |
134 | } | |
135 | ||
136 | lst::byte_order lst::type::reverse_byte_order(lst::byte_order byte_order) noexcept | |
137 | { | |
138 | if (byte_order == lst::byte_order::BIG_ENDIAN_) { | |
139 | return lst::byte_order::LITTLE_ENDIAN_; | |
140 | } else { | |
141 | return lst::byte_order::BIG_ENDIAN_; | |
142 | } | |
143 | } | |
144 | ||
145 | lst::floating_point_type::floating_point_type(unsigned int in_alignment, | |
28ab034a JG |
146 | lst::byte_order in_byte_order, |
147 | unsigned int in_exponent_digits, | |
148 | unsigned int in_mantissa_digits) : | |
0220be14 JG |
149 | type(in_alignment), |
150 | byte_order(in_byte_order), | |
28ab034a | 151 | exponent_digits{ in_exponent_digits }, |
0220be14 JG |
152 | mantissa_digits(in_mantissa_digits) |
153 | { | |
154 | /* Allowed (exponent, mantissa) pairs. */ | |
d7bfb9b0 | 155 | static const std::set<std::pair<unsigned int, unsigned int>> allowed_pairs{ |
28ab034a JG |
156 | { 5, 11 }, /* binary16 */ |
157 | { 8, 24 }, /* binary32 */ | |
158 | { 11, 53 }, /* binary64 */ | |
159 | { 15, 113 }, /* binary128 */ | |
0220be14 JG |
160 | }; |
161 | ||
28ab034a | 162 | if (allowed_pairs.find({ exponent_digits, mantissa_digits }) != allowed_pairs.end()) { |
d7bfb9b0 JG |
163 | /* mantissa and exponent digits is a valid pair. */ |
164 | return; | |
0220be14 JG |
165 | } |
166 | ||
28ab034a JG |
167 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( |
168 | "Invalid exponent/mantissa values provided while creating {}", typeid(*this))); | |
0220be14 JG |
169 | } |
170 | ||
b6bbb1d6 JG |
171 | lst::type::cuptr lst::floating_point_type::copy() const |
172 | { | |
173 | return lttng::make_unique<floating_point_type>( | |
28ab034a | 174 | alignment, byte_order, exponent_digits, mantissa_digits); |
b6bbb1d6 JG |
175 | } |
176 | ||
0220be14 JG |
177 | void lst::floating_point_type::accept(type_visitor& visitor) const |
178 | { | |
179 | visitor.visit(*this); | |
180 | } | |
181 | ||
182 | bool lst::floating_point_type::_is_equal(const type& base_other) const noexcept | |
183 | { | |
184 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
185 | ||
186 | return this->byte_order == other.byte_order && | |
28ab034a JG |
187 | this->exponent_digits == other.exponent_digits && |
188 | this->mantissa_digits == other.mantissa_digits; | |
0220be14 JG |
189 | } |
190 | ||
191 | lst::enumeration_type::enumeration_type(unsigned int in_alignment, | |
28ab034a JG |
192 | enum lst::byte_order in_byte_order, |
193 | unsigned int in_size, | |
194 | enum signedness in_signedness, | |
195 | enum base in_base, | |
196 | lst::integer_type::roles in_roles) : | |
197 | integer_type( | |
198 | in_alignment, in_byte_order, in_size, in_signedness, in_base, std::move(in_roles)) | |
0220be14 JG |
199 | { |
200 | } | |
201 | ||
7a73918f JG |
202 | /* |
203 | * Due to a bug in g++ < 7.1, these specializations must be enclosed in the namespaces | |
204 | * rather than using the usual `namespace::namespace::function` notation: | |
205 | * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480. | |
206 | */ | |
e2c2bec2 JR |
207 | namespace lttng { |
208 | namespace sessiond { | |
209 | namespace trace { | |
0220be14 | 210 | template <> |
7a73918f | 211 | void signed_enumeration_type::accept(type_visitor& visitor) const |
0220be14 JG |
212 | { |
213 | visitor.visit(*this); | |
214 | } | |
215 | ||
216 | template <> | |
7a73918f | 217 | void unsigned_enumeration_type::accept(type_visitor& visitor) const |
0220be14 JG |
218 | { |
219 | visitor.visit(*this); | |
220 | } | |
221 | ||
45110cdd | 222 | template <> |
7a73918f | 223 | void variant_type<lst::signed_enumeration_type::mapping::range_t::range_integer_t>::accept( |
28ab034a | 224 | lst::type_visitor& visitor) const |
45110cdd JG |
225 | { |
226 | visitor.visit(*this); | |
227 | } | |
228 | ||
229 | template <> | |
7a73918f | 230 | void variant_type<lst::unsigned_enumeration_type::mapping::range_t::range_integer_t>::accept( |
28ab034a | 231 | lst::type_visitor& visitor) const |
45110cdd JG |
232 | { |
233 | visitor.visit(*this); | |
234 | } | |
7a73918f JG |
235 | } /* namespace trace */ |
236 | } /* namespace sessiond */ | |
237 | } /* namespace lttng */ | |
45110cdd | 238 | |
0220be14 | 239 | lst::array_type::array_type(unsigned int in_alignment, type::cuptr in_element_type) : |
28ab034a | 240 | type(in_alignment), element_type{ std::move(in_element_type) } |
0220be14 JG |
241 | { |
242 | } | |
243 | ||
244 | bool lst::array_type::_is_equal(const type& base_other) const noexcept | |
245 | { | |
246 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
247 | ||
248 | return *this->element_type == *other.element_type; | |
249 | } | |
250 | ||
251 | lst::static_length_array_type::static_length_array_type(unsigned int in_alignment, | |
28ab034a JG |
252 | type::cuptr in_element_type, |
253 | uint64_t in_length) : | |
254 | array_type(in_alignment, std::move(in_element_type)), length{ in_length } | |
0220be14 JG |
255 | { |
256 | } | |
257 | ||
258 | bool lst::static_length_array_type::_is_equal(const type& base_other) const noexcept | |
259 | { | |
260 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
261 | ||
262 | return array_type::_is_equal(base_other) && this->length == other.length; | |
263 | } | |
264 | ||
b6bbb1d6 JG |
265 | lst::type::cuptr lst::static_length_array_type::copy() const |
266 | { | |
267 | return lttng::make_unique<static_length_array_type>( | |
28ab034a | 268 | alignment, element_type->copy(), length); |
b6bbb1d6 JG |
269 | } |
270 | ||
0220be14 JG |
271 | void lst::static_length_array_type::accept(type_visitor& visitor) const |
272 | { | |
273 | visitor.visit(*this); | |
274 | } | |
275 | ||
28ab034a JG |
276 | lst::dynamic_length_array_type::dynamic_length_array_type( |
277 | unsigned int in_alignment, | |
278 | type::cuptr in_element_type, | |
279 | lst::field_location in_length_field_location) : | |
0220be14 | 280 | array_type(in_alignment, std::move(in_element_type)), |
28ab034a | 281 | length_field_location{ std::move(in_length_field_location) } |
0220be14 JG |
282 | { |
283 | } | |
284 | ||
285 | bool lst::dynamic_length_array_type::_is_equal(const type& base_other) const noexcept | |
286 | { | |
287 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
288 | ||
289 | return array_type::_is_equal(base_other) && | |
28ab034a | 290 | this->length_field_location == other.length_field_location; |
0220be14 JG |
291 | } |
292 | ||
b6bbb1d6 JG |
293 | lst::type::cuptr lst::dynamic_length_array_type::copy() const |
294 | { | |
295 | return lttng::make_unique<dynamic_length_array_type>( | |
28ab034a | 296 | alignment, element_type->copy(), length_field_location); |
b6bbb1d6 JG |
297 | } |
298 | ||
0220be14 JG |
299 | void lst::dynamic_length_array_type::accept(type_visitor& visitor) const |
300 | { | |
301 | visitor.visit(*this); | |
302 | } | |
303 | ||
28ab034a JG |
304 | lst::static_length_blob_type::static_length_blob_type(unsigned int in_alignment, |
305 | uint64_t in_length_bytes, | |
306 | roles in_roles) : | |
307 | type(in_alignment), length_bytes{ in_length_bytes }, roles_{ std::move(in_roles) } | |
e7360180 JG |
308 | { |
309 | } | |
310 | ||
311 | bool lst::static_length_blob_type::_is_equal(const type& base_other) const noexcept | |
312 | { | |
313 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
314 | ||
315 | return length_bytes == other.length_bytes && roles_ == other.roles_; | |
316 | } | |
317 | ||
b6bbb1d6 JG |
318 | lst::type::cuptr lst::static_length_blob_type::copy() const |
319 | { | |
320 | return lttng::make_unique<static_length_blob_type>(alignment, length_bytes, roles_); | |
321 | } | |
322 | ||
e7360180 JG |
323 | void lst::static_length_blob_type::accept(type_visitor& visitor) const |
324 | { | |
325 | visitor.visit(*this); | |
326 | } | |
327 | ||
328 | lst::dynamic_length_blob_type::dynamic_length_blob_type( | |
28ab034a JG |
329 | unsigned int in_alignment, lst::field_location in_length_field_location) : |
330 | type(in_alignment), length_field_location{ std::move(in_length_field_location) } | |
e7360180 JG |
331 | { |
332 | } | |
333 | ||
334 | bool lst::dynamic_length_blob_type::_is_equal(const type& base_other) const noexcept | |
335 | { | |
336 | const auto& other = dynamic_cast<decltype(*this)&>(base_other); | |
337 | ||
eda1aa02 | 338 | return length_field_location == other.length_field_location; |
e7360180 JG |
339 | } |
340 | ||
b6bbb1d6 JG |
341 | lst::type::cuptr lst::dynamic_length_blob_type::copy() const |
342 | { | |
343 | return lttng::make_unique<dynamic_length_blob_type>(alignment, length_field_location); | |
344 | } | |
345 | ||
e7360180 JG |
346 | void lst::dynamic_length_blob_type::accept(type_visitor& visitor) const |
347 | { | |
348 | visitor.visit(*this); | |
349 | } | |
350 | ||
0220be14 | 351 | lst::string_type::string_type(unsigned int in_alignment, enum encoding in_encoding) : |
28ab034a | 352 | type(in_alignment), encoding_{ in_encoding } |
0220be14 JG |
353 | { |
354 | } | |
355 | ||
356 | bool lst::string_type::_is_equal(const type& base_other) const noexcept | |
357 | { | |
358 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
359 | ||
65cd3c0c | 360 | return this->encoding_ == other.encoding_; |
0220be14 JG |
361 | } |
362 | ||
28ab034a JG |
363 | lst::static_length_string_type::static_length_string_type(unsigned int in_alignment, |
364 | enum encoding in_encoding, | |
365 | uint64_t in_length) : | |
366 | string_type(in_alignment, in_encoding), length{ in_length } | |
0220be14 JG |
367 | { |
368 | } | |
369 | ||
370 | bool lst::static_length_string_type::_is_equal(const type& base_other) const noexcept | |
371 | { | |
372 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
373 | ||
374 | return string_type::_is_equal(base_other) && this->length == other.length; | |
375 | } | |
376 | ||
b6bbb1d6 JG |
377 | lst::type::cuptr lst::static_length_string_type::copy() const |
378 | { | |
379 | return lttng::make_unique<static_length_string_type>(alignment, encoding_, length); | |
380 | } | |
381 | ||
0220be14 JG |
382 | void lst::static_length_string_type::accept(type_visitor& visitor) const |
383 | { | |
384 | visitor.visit(*this); | |
385 | } | |
386 | ||
28ab034a JG |
387 | lst::dynamic_length_string_type::dynamic_length_string_type( |
388 | unsigned int in_alignment, | |
389 | enum encoding in_encoding, | |
390 | field_location in_length_field_location) : | |
eda1aa02 | 391 | string_type(in_alignment, in_encoding), |
28ab034a | 392 | length_field_location{ std::move(in_length_field_location) } |
0220be14 JG |
393 | { |
394 | } | |
395 | ||
396 | bool lst::dynamic_length_string_type::_is_equal(const type& base_other) const noexcept | |
397 | { | |
398 | const auto& other = static_cast<decltype(*this)&>(base_other); | |
399 | ||
400 | return string_type::_is_equal(base_other) && | |
28ab034a | 401 | this->length_field_location == other.length_field_location; |
0220be14 JG |
402 | } |
403 | ||
b6bbb1d6 JG |
404 | lst::type::cuptr lst::dynamic_length_string_type::copy() const |
405 | { | |
406 | return lttng::make_unique<dynamic_length_string_type>( | |
28ab034a | 407 | alignment, encoding_, length_field_location); |
b6bbb1d6 JG |
408 | } |
409 | ||
0220be14 JG |
410 | void lst::dynamic_length_string_type::accept(type_visitor& visitor) const |
411 | { | |
412 | visitor.visit(*this); | |
413 | } | |
414 | ||
415 | lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment, | |
28ab034a | 416 | enum encoding in_encoding) : |
0220be14 JG |
417 | string_type(in_alignment, in_encoding) |
418 | { | |
419 | } | |
420 | ||
b6bbb1d6 JG |
421 | lst::type::cuptr lst::null_terminated_string_type::copy() const |
422 | { | |
423 | return lttng::make_unique<null_terminated_string_type>(alignment, encoding_); | |
424 | } | |
425 | ||
0220be14 JG |
426 | void lst::null_terminated_string_type::accept(type_visitor& visitor) const |
427 | { | |
428 | visitor.visit(*this); | |
429 | } | |
430 | ||
431 | lst::structure_type::structure_type(unsigned int in_alignment, fields in_fields) : | |
28ab034a | 432 | type(in_alignment), fields_{ std::move(in_fields) } |
0220be14 JG |
433 | { |
434 | } | |
435 | ||
436 | bool lst::structure_type::_is_equal(const type& base_other) const noexcept | |
437 | { | |
28ab034a | 438 | const auto& other = static_cast<decltype(*this)&>(base_other); |
0220be14 | 439 | |
da9dd521 | 440 | return fields_are_equal(this->fields_, other.fields_); |
0220be14 JG |
441 | } |
442 | ||
b6bbb1d6 JG |
443 | lst::type::cuptr lst::structure_type::copy() const |
444 | { | |
445 | structure_type::fields copy_of_fields; | |
446 | ||
447 | copy_of_fields.reserve(fields_.size()); | |
448 | for (const auto& field : fields_) { | |
28ab034a JG |
449 | copy_of_fields.emplace_back( |
450 | lttng::make_unique<lst::field>(field->name, field->get_type().copy())); | |
b6bbb1d6 JG |
451 | } |
452 | ||
453 | return lttng::make_unique<structure_type>(alignment, std::move(copy_of_fields)); | |
454 | } | |
455 | ||
0220be14 JG |
456 | void lst::structure_type::accept(type_visitor& visitor) const |
457 | { | |
458 | visitor.visit(*this); | |
7a73918f | 459 | } |