From 661c29a7835cf1755deaf21f91832f00e958a318 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 31 Jul 2026 10:20:56 +0200 Subject: Review spaceship usage --- boot/evaluator.cc | 101 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 26 deletions(-) (limited to 'boot/evaluator.cc') diff --git a/boot/evaluator.cc b/boot/evaluator.cc index b49a9ec..bff727d 100644 --- a/boot/evaluator.cc +++ b/boot/evaluator.cc @@ -218,7 +218,7 @@ namespace elna::boot } if (is_primitive_type(decoration, "Word")) { - return constant_value{ static_cast&>(subject).value }; + return constant_value{ static_cast&>(subject).value }; } if (is_primitive_type(decoration, "Float")) { @@ -279,9 +279,9 @@ namespace elna::boot } position = static_cast(*int_index); } - else if (auto *word_index = std::get_if(&index.value())) + else if (auto *word_index = std::get_if(&index.value())) { - position = *word_index; + position = word_index->to(); } else { @@ -416,7 +416,7 @@ namespace elna::boot return std::visit([](const auto& value) -> std::optional { using T = std::decay_t; - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral || std::is_same_v) { return constant_value{ ~value }; } @@ -433,7 +433,7 @@ namespace elna::boot template static std::optional add_overflow(T lhs, T rhs) { - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral) { T result; return __builtin_add_overflow(lhs, rhs, &result) @@ -446,7 +446,7 @@ namespace elna::boot template static std::optional sub_overflow(T lhs, T rhs) { - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral) { T result; return __builtin_sub_overflow(lhs, rhs, &result) @@ -459,7 +459,7 @@ namespace elna::boot template static std::optional mul_overflow(T lhs, T rhs) { - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral) { T result; return __builtin_mul_overflow(lhs, rhs, &result) @@ -506,7 +506,14 @@ namespace elna::boot { using enum binary_operator; case sum: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.add(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (is_arithmetic) { if (auto result = add_overflow(lhs, rhs)) { @@ -515,7 +522,14 @@ namespace elna::boot } return std::nullopt; case subtraction: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.sub(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (is_arithmetic) { if (auto result = sub_overflow(lhs, rhs)) { @@ -524,7 +538,14 @@ namespace elna::boot } return std::nullopt; case multiplication: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.mul(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (is_arithmetic) { if (auto result = mul_overflow(lhs, rhs)) { @@ -533,7 +554,14 @@ namespace elna::boot } return std::nullopt; case division: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.div(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (is_arithmetic) { if (rhs != static_cast(0)) { @@ -542,7 +570,14 @@ namespace elna::boot } return std::nullopt; case remainder: - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.mod(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (is_integral) { if (rhs != static_cast(0)) { @@ -552,21 +587,21 @@ namespace elna::boot return std::nullopt; case disjunction: case bitwise_disjunction: - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral || std::is_same_v) { return constant_value{ lhs | rhs }; } return std::nullopt; case conjunction: case bitwise_conjunction: - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral || std::is_same_v) { return constant_value{ lhs & rhs }; } return std::nullopt; case exclusive_disjunction: case bitwise_exclusive_disjunction: - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (is_integral || std::is_same_v) { return constant_value{ lhs ^ rhs }; } @@ -590,7 +625,14 @@ namespace elna::boot } return std::nullopt; case shift_left: - if constexpr (std::is_integral_v && !std::is_same_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.shl(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (is_integral) { if (rhs < 0 || static_cast>(rhs) >= std::numeric_limits::digits) { @@ -600,7 +642,14 @@ namespace elna::boot } return std::nullopt; case shift_right: - if constexpr (std::is_integral_v) + if constexpr (std::is_same_v) + { + if (auto result = lhs.shr(rhs)) + { + return constant_value{ result.value() }; + } + } + else if constexpr (std::is_integral_v) { return constant_value{ lhs >> rhs }; } @@ -610,25 +659,25 @@ namespace elna::boot case not_equals: return constant_value{ lhs != rhs }; case less: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (is_arithmetic || std::is_same_v) { return constant_value{ lhs < rhs }; } return std::nullopt; case greater: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (is_arithmetic || std::is_same_v) { return constant_value{ lhs > rhs }; } return std::nullopt; case less_equal: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (is_arithmetic || std::is_same_v) { return constant_value{ lhs <= rhs }; } return std::nullopt; case greater_equal: - if constexpr (std::is_arithmetic_v && !std::is_same_v) + if constexpr (is_arithmetic || std::is_same_v) { return constant_value{ lhs >= rhs }; } @@ -690,14 +739,14 @@ namespace elna::boot { if (auto size = evaluate_traits_size(subject.types.front())) { - return constant_value{ static_cast(size.value()) }; + return constant_value{ integer_literal::from(static_cast(size.value())) }; } } else if (subject.name.name() == "alignment") { if (auto alignment = evaluate_traits_alignment(subject.types.front())) { - return constant_value{ static_cast(alignment.value()) }; + return constant_value{ integer_literal::from(static_cast(alignment.value())) }; } } else if (subject.name.name() == "min") @@ -710,7 +759,7 @@ namespace elna::boot } if (is_primitive_type(resolved, "Word")) { - return constant_value{ static_cast(0) }; + return constant_value{ integer_literal::from(static_cast(0)) }; } if (is_primitive_type(resolved, "Char")) { @@ -739,7 +788,7 @@ namespace elna::boot } if (is_primitive_type(resolved, "Word")) { - return constant_value{ std::numeric_limits::max() }; + return constant_value{ integer_literal::from(std::numeric_limits::max()) }; } if (is_primitive_type(resolved, "Char")) { @@ -772,7 +821,7 @@ namespace elna::boot if (field_search != std::cend(record_layout.value().offset_map)) { - return constant_value{ static_cast(field_search->second) }; + return constant_value{ integer_literal::from(static_cast(field_search->second)) }; } } } -- cgit v1.2.3