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