| 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 | |
| 13 | #include <set> |
| 14 | |
| 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 | |
| 33 | lst::type::type(unsigned int in_alignment) : alignment{in_alignment} |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | lst::type::~type() |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | bool lst::type::operator==(const lst::type& other) const noexcept |
| 42 | { |
| 43 | return typeid(*this) == typeid(other) && |
| 44 | alignment == other.alignment && |
| 45 | /* defer to concrete type comparison */ |
| 46 | this->_is_equal(other); |
| 47 | } |
| 48 | |
| 49 | bool lst::type::operator!=(const lst::type& other) const noexcept |
| 50 | { |
| 51 | return !(*this == other); |
| 52 | } |
| 53 | |
| 54 | lst::field::field(std::string in_name, lst::type::cuptr in_type) : |
| 55 | name{std::move(in_name)}, _type{std::move(in_type)} |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void lst::field::accept(lst::field_visitor& visitor) const |
| 60 | { |
| 61 | visitor.visit(*this); |
| 62 | } |
| 63 | |
| 64 | bool lst::field::operator==(const lst::field& other) const noexcept |
| 65 | { |
| 66 | return name == other.name && *_type == *other._type; |
| 67 | } |
| 68 | |
| 69 | lst::integer_type::integer_type(unsigned int in_alignment, |
| 70 | enum lst::byte_order in_byte_order, |
| 71 | unsigned int in_size, |
| 72 | enum lst::integer_type::signedness in_signedness, |
| 73 | enum lst::integer_type::base in_base) : |
| 74 | type(in_alignment), |
| 75 | byte_order{in_byte_order}, |
| 76 | size{in_size}, |
| 77 | signedness_{in_signedness}, |
| 78 | base_{in_base} |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | bool lst::integer_type::_is_equal(const type &base_other) const noexcept |
| 83 | { |
| 84 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 85 | |
| 86 | return this->byte_order == other.byte_order && |
| 87 | this->size == other.size && |
| 88 | this->signedness_ == other.signedness_ && |
| 89 | this->base_ == other.base_; |
| 90 | } |
| 91 | |
| 92 | void lst::integer_type::accept(type_visitor& visitor) const |
| 93 | { |
| 94 | visitor.visit(*this); |
| 95 | } |
| 96 | |
| 97 | lst::byte_order lst::type::reverse_byte_order(lst::byte_order byte_order) noexcept |
| 98 | { |
| 99 | if (byte_order == lst::byte_order::BIG_ENDIAN_) { |
| 100 | return lst::byte_order::LITTLE_ENDIAN_; |
| 101 | } else { |
| 102 | return lst::byte_order::BIG_ENDIAN_; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | lst::floating_point_type::floating_point_type(unsigned int in_alignment, |
| 107 | lst::byte_order in_byte_order, |
| 108 | unsigned int in_exponent_digits, |
| 109 | unsigned int in_mantissa_digits) : |
| 110 | type(in_alignment), |
| 111 | byte_order(in_byte_order), |
| 112 | exponent_digits{in_exponent_digits}, |
| 113 | mantissa_digits(in_mantissa_digits) |
| 114 | { |
| 115 | /* Allowed (exponent, mantissa) pairs. */ |
| 116 | static const std::set<std::pair<unsigned int, unsigned int>> allowed_pairs{ |
| 117 | {5, 11}, /* binary16 */ |
| 118 | {8, 24}, /* binary32 */ |
| 119 | {11, 53}, /* binary64 */ |
| 120 | {15, 113}, /* binary128 */ |
| 121 | }; |
| 122 | |
| 123 | if (allowed_pairs.find({exponent_digits, mantissa_digits}) != allowed_pairs.end()) { |
| 124 | /* mantissa and exponent digits is a valid pair. */ |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | LTTNG_THROW_INVALID_ARGUMENT_ERROR( |
| 129 | fmt::format("Invalid exponent/mantissa values provided while creating {}", |
| 130 | typeid(*this))); |
| 131 | } |
| 132 | |
| 133 | void lst::floating_point_type::accept(type_visitor& visitor) const |
| 134 | { |
| 135 | visitor.visit(*this); |
| 136 | } |
| 137 | |
| 138 | bool lst::floating_point_type::_is_equal(const type& base_other) const noexcept |
| 139 | { |
| 140 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 141 | |
| 142 | return this->byte_order == other.byte_order && |
| 143 | this->exponent_digits == other.exponent_digits && |
| 144 | this->mantissa_digits == other.mantissa_digits; |
| 145 | } |
| 146 | |
| 147 | lst::enumeration_type::enumeration_type(unsigned int in_alignment, |
| 148 | enum lst::byte_order in_byte_order, |
| 149 | unsigned int in_size, |
| 150 | enum signedness in_signedness, |
| 151 | enum base in_base) : |
| 152 | integer_type(in_alignment, in_byte_order, in_size, in_signedness, in_base) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | template <> |
| 157 | void lst::signed_enumeration_type::accept(type_visitor& visitor) const |
| 158 | { |
| 159 | visitor.visit(*this); |
| 160 | } |
| 161 | |
| 162 | template <> |
| 163 | void lst::unsigned_enumeration_type::accept(type_visitor& visitor) const |
| 164 | { |
| 165 | visitor.visit(*this); |
| 166 | } |
| 167 | |
| 168 | lst::array_type::array_type(unsigned int in_alignment, type::cuptr in_element_type) : |
| 169 | type(in_alignment), element_type{std::move(in_element_type)} |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | bool lst::array_type::_is_equal(const type& base_other) const noexcept |
| 174 | { |
| 175 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 176 | |
| 177 | return *this->element_type == *other.element_type; |
| 178 | } |
| 179 | |
| 180 | lst::static_length_array_type::static_length_array_type(unsigned int in_alignment, |
| 181 | type::cuptr in_element_type, |
| 182 | uint64_t in_length) : |
| 183 | array_type(in_alignment, std::move(in_element_type)), |
| 184 | length{in_length} |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | bool lst::static_length_array_type::_is_equal(const type& base_other) const noexcept |
| 189 | { |
| 190 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 191 | |
| 192 | return array_type::_is_equal(base_other) && this->length == other.length; |
| 193 | } |
| 194 | |
| 195 | void lst::static_length_array_type::accept(type_visitor& visitor) const |
| 196 | { |
| 197 | visitor.visit(*this); |
| 198 | } |
| 199 | |
| 200 | lst::dynamic_length_array_type::dynamic_length_array_type(unsigned int in_alignment, |
| 201 | type::cuptr in_element_type, |
| 202 | std::string in_length_field_name) : |
| 203 | array_type(in_alignment, std::move(in_element_type)), |
| 204 | length_field_name{std::move(in_length_field_name)} |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | bool lst::dynamic_length_array_type::_is_equal(const type& base_other) const noexcept |
| 209 | { |
| 210 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 211 | |
| 212 | return array_type::_is_equal(base_other) && |
| 213 | this->length_field_name == other.length_field_name; |
| 214 | } |
| 215 | |
| 216 | void lst::dynamic_length_array_type::accept(type_visitor& visitor) const |
| 217 | { |
| 218 | visitor.visit(*this); |
| 219 | } |
| 220 | |
| 221 | lst::string_type::string_type(unsigned int in_alignment, enum encoding in_encoding) : |
| 222 | type(in_alignment), encoding_{in_encoding} |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | bool lst::string_type::_is_equal(const type& base_other) const noexcept |
| 227 | { |
| 228 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 229 | |
| 230 | return this->encoding_ == other.encoding_; |
| 231 | } |
| 232 | |
| 233 | lst::static_length_string_type::static_length_string_type( |
| 234 | unsigned int in_alignment, enum encoding in_encoding, uint64_t in_length) : |
| 235 | string_type(in_alignment, in_encoding), length{in_length} |
| 236 | { |
| 237 | } |
| 238 | |
| 239 | bool lst::static_length_string_type::_is_equal(const type& base_other) const noexcept |
| 240 | { |
| 241 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 242 | |
| 243 | return string_type::_is_equal(base_other) && this->length == other.length; |
| 244 | } |
| 245 | |
| 246 | void lst::static_length_string_type::accept(type_visitor& visitor) const |
| 247 | { |
| 248 | visitor.visit(*this); |
| 249 | } |
| 250 | |
| 251 | lst::dynamic_length_string_type::dynamic_length_string_type(unsigned int in_alignment, |
| 252 | enum encoding in_encoding, |
| 253 | std::string in_length_field_name) : |
| 254 | string_type(in_alignment, in_encoding), length_field_name{std::move(in_length_field_name)} |
| 255 | { |
| 256 | } |
| 257 | |
| 258 | bool lst::dynamic_length_string_type::_is_equal(const type& base_other) const noexcept |
| 259 | { |
| 260 | const auto& other = static_cast<decltype(*this)&>(base_other); |
| 261 | |
| 262 | return string_type::_is_equal(base_other) && |
| 263 | this->length_field_name == other.length_field_name; |
| 264 | } |
| 265 | |
| 266 | void lst::dynamic_length_string_type::accept(type_visitor& visitor) const |
| 267 | { |
| 268 | visitor.visit(*this); |
| 269 | } |
| 270 | |
| 271 | lst::null_terminated_string_type::null_terminated_string_type(unsigned int in_alignment, |
| 272 | enum encoding in_encoding) : |
| 273 | string_type(in_alignment, in_encoding) |
| 274 | { |
| 275 | } |
| 276 | |
| 277 | void lst::null_terminated_string_type::accept(type_visitor& visitor) const |
| 278 | { |
| 279 | visitor.visit(*this); |
| 280 | } |
| 281 | |
| 282 | lst::structure_type::structure_type(unsigned int in_alignment, fields in_fields) : |
| 283 | type(in_alignment), _fields{std::move(in_fields)} |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | bool lst::structure_type::_is_equal(const type& base_other) const noexcept |
| 288 | { |
| 289 | const auto &other = static_cast<decltype(*this)&>(base_other); |
| 290 | |
| 291 | return fields_are_equal(this->_fields, other._fields); |
| 292 | } |
| 293 | |
| 294 | void lst::structure_type::accept(type_visitor& visitor) const |
| 295 | { |
| 296 | visitor.visit(*this); |
| 297 | } |
| 298 | |
| 299 | lst::variant_type::variant_type(unsigned int in_alignment, |
| 300 | std::string in_tag_name, |
| 301 | choices in_choices) : |
| 302 | type(in_alignment), |
| 303 | tag_name{std::move(in_tag_name)}, |
| 304 | _choices{std::move(in_choices)} |
| 305 | { |
| 306 | } |
| 307 | |
| 308 | bool lst::variant_type::_is_equal(const type& base_other) const noexcept |
| 309 | { |
| 310 | const auto &other = static_cast<decltype(*this)&>(base_other); |
| 311 | |
| 312 | return this->tag_name == other.tag_name && |
| 313 | fields_are_equal(this->_choices, other._choices); |
| 314 | } |
| 315 | |
| 316 | void lst::variant_type::accept(type_visitor& visitor) const |
| 317 | { |
| 318 | visitor.visit(*this); |
| 319 | } |