Skip to content

Commit

Permalink
refactor: Remove C++ 20 workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored May 21, 2024
1 parent ad0c447 commit 08b4313
Show file tree
Hide file tree
Showing 20 changed files with 63 additions and 230 deletions.
13 changes: 0 additions & 13 deletions language_server/src/feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,7 @@ class request_id
: id(std::move(s))
{}

#ifdef __cpp_lib_three_way_comparison
auto operator<=>(const request_id&) const = default;
#else
bool operator==(const request_id& o) const { return id == o.id; }
std::strong_ordering operator<=>(const request_id& o) const
{
if (auto c = id.index() <=> o.id.index(); c != 0)
return c;
if (std::holds_alternative<long>(id))
return std::get<long>(id) <=> std::get<long>(o.id);
else
return std::get<std::string>(id).compare(std::get<std::string>(o.id)) <=> 0;
}
#endif

std::string to_string() const
{
Expand Down
8 changes: 0 additions & 8 deletions parser_library/include/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,6 @@ struct token_info

struct output_line
{
output_line(int level, std::string text)
: level(level)
, text(std::move(text))
{} // clang-14
output_line(int level, std::string_view text)
: level(level)
, text(text)
{} // clang-14
int level;
std::string text;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ uint32_t get_X_B_length_attr(const std::string& s, uint64_t frac)
}

// Checks comma separated values. is_valid_digit specifies whether the char is valid character of value.
template</* std::predicate<char> */ typename F>
template<std::predicate<char> F>
bool check_comma_separated(const std::string& nom, F is_valid_digit)
{
bool last_valid = false;
Expand Down
26 changes: 0 additions & 26 deletions parser_library/src/config/assembler_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ constexpr std::array<instr_set_equivalent_pair, 25> instr_set_optable_equivalent
{ std::string_view("ZSA"), instruction_set_version::Z16 },
} };

#if __cpp_lib_ranges
static_assert(std::ranges::is_sorted(instr_set_optable_equivalents, {}, &instr_set_equivalent_pair::first));
#else
static_assert(std::is_sorted(std::begin(instr_set_optable_equivalents),
std::end(instr_set_optable_equivalents),
[](const auto& l, const auto& r) { return l.first < r.first; }));
#endif

constexpr std::array<instr_set_equivalent_pair, 58> instr_set_machine_equivalents = { {
{ std::string_view("ARCH-0"), instruction_set_version::XA },
Expand Down Expand Up @@ -123,28 +117,15 @@ constexpr std::array<instr_set_equivalent_pair, 58> instr_set_machine_equivalent
{ std::string_view("ZSERIES-9"), instruction_set_version::Z15 },
} };

#if __cpp_lib_ranges
static_assert(std::ranges::is_sorted(instr_set_machine_equivalents, {}, &instr_set_equivalent_pair::first));
#else
static_assert(std::is_sorted(std::begin(instr_set_machine_equivalents),
std::end(instr_set_machine_equivalents),
[](const auto& l, const auto& r) { return l.first < r.first; }));
#endif

bool instr_set_equivalent_valid(
std::string instr_set_name, std::span<const instr_set_equivalent_pair> equivalents) noexcept
{
utils::to_upper(instr_set_name);

#ifdef __cpp_lib_ranges
return instr_set_name.size() == 0
|| std::ranges::any_of(equivalents, [&instr_set_name](auto item) { return instr_set_name == item.first; });
#else
return instr_set_name.size() == 0
|| std::any_of(std::begin(equivalents), std::end(equivalents), [&instr_set_name](auto item) {
return instr_set_name == item.first;
});
#endif
}
} // namespace

Expand All @@ -167,14 +148,7 @@ std::optional<instruction_set_version> find_instruction_set(
{
utils::to_upper(instr_set_name);

#ifdef __cpp_lib_ranges
auto it = std::ranges::lower_bound(equivalents, instr_set_name, {}, [](const auto& instr) { return instr.first; });
#else
auto it = std::lower_bound(
std::begin(equivalents), std::end(equivalents), instr_set_name, [](const auto& l, const auto& r) {
return l.first < r;
});
#endif

if (it == std::end(equivalents) || it->first != instr_set_name)
return std::nullopt;
Expand Down
81 changes: 2 additions & 79 deletions parser_library/src/context/instruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ constexpr ca_instruction ca_instructions[] = {
{ "SETB", false },
{ "SETC", false },
};
#if __cpp_lib_ranges

static_assert(std::ranges::is_sorted(ca_instructions, {}, &ca_instruction::name));

const ca_instruction* instruction::find_ca_instructions(std::string_view name) noexcept
Expand All @@ -295,23 +295,6 @@ const ca_instruction* instruction::find_ca_instructions(std::string_view name) n
return nullptr;
return std::to_address(it);
}
#else
static_assert(std::is_sorted(std::begin(ca_instructions), std::end(ca_instructions), [](const auto& l, const auto& r) {
return l.name() < r.name();
}));

const ca_instruction* instruction::find_ca_instructions(std::string_view name) noexcept
{
auto it = std::lower_bound(
std::begin(ca_instructions), std::end(ca_instructions), name, [](const auto& l, const auto& r) {
return l.name() < r;
});

if (it == std::end(ca_instructions) || it->name() != name)
return nullptr;
return std::to_address(it);
}
#endif

const ca_instruction& instruction::get_ca_instructions(std::string_view name) noexcept
{
Expand Down Expand Up @@ -376,7 +359,7 @@ constexpr assembler_instruction assembler_instructions[] = {
{ "XATTR", 1, -1, false, "attribute+" },

};
#ifdef __cpp_lib_ranges

static_assert(std::ranges::is_sorted(assembler_instructions, {}, &assembler_instruction::name));

const assembler_instruction* instruction::find_assembler_instructions(std::string_view instr) noexcept
Expand All @@ -386,22 +369,6 @@ const assembler_instruction* instruction::find_assembler_instructions(std::strin
return nullptr;
return std::to_address(it);
}
#else
static_assert(std::is_sorted(std::begin(assembler_instructions),
std::end(assembler_instructions),
[](const auto& l, const auto& r) { return l.name() < r.name(); }));

const assembler_instruction* instruction::find_assembler_instructions(std::string_view instr) noexcept
{
auto it = std::lower_bound(
std::begin(assembler_instructions), std::end(assembler_instructions), instr, [](const auto& l, const auto& r) {
return l.name() < r;
});
if (it == std::end(assembler_instructions) || it->name() != instr)
return nullptr;
return std::to_address(it);
}
#endif

const assembler_instruction& instruction::get_assembler_instructions(std::string_view instr) noexcept
{
Expand Down Expand Up @@ -565,7 +532,6 @@ constexpr machine_instruction machine_instructions[] = {
#include "instruction_details.h"
};

#ifdef __cpp_lib_ranges
static_assert(std::ranges::is_sorted(machine_instructions, {}, &machine_instruction::name));

const machine_instruction* instruction::find_machine_instructions(std::string_view name) noexcept
Expand All @@ -582,32 +548,6 @@ constexpr const machine_instruction* find_mi(std::string_view name)
assert(it != std::ranges::end(machine_instructions) && it->name() == name);
return std::to_address(it);
}
#else
static_assert(std::is_sorted(std::begin(machine_instructions),
std::end(machine_instructions),
[](const auto& l, const auto& r) { return l.name() < r.name(); }));

const machine_instruction* instruction::find_machine_instructions(std::string_view name) noexcept
{
auto it = std::lower_bound(
std::begin(machine_instructions), std::end(machine_instructions), name, [](const auto& l, const auto& r) {
return l.name() < r;
});
if (it == std::end(machine_instructions) || it->name() != name)
return nullptr;
return std::to_address(it);
}

constexpr const machine_instruction* find_mi(std::string_view name)
{
auto it = std::lower_bound(
std::begin(machine_instructions), std::end(machine_instructions), name, [](const auto& l, const auto& r) {
return l.name() < r;
});
assert(it != std::end(machine_instructions) && it->name() == name);
return std::to_address(it);
}
#endif

const machine_instruction& instruction::get_machine_instructions(std::string_view name) noexcept
{
Expand Down Expand Up @@ -2011,7 +1951,6 @@ constexpr mnemonic_code mnemonic_codes[] = {
{ "XLHR", mi_RXSBG, { { 2, 32 }, { 63 }, { 32 } }, UNI_SINCE_Z11 },
};

#ifdef __cpp_lib_ranges
static_assert(std::ranges::is_sorted(mnemonic_codes, {}, &mnemonic_code::name));

const mnemonic_code* instruction::find_mnemonic_codes(std::string_view name) noexcept
Expand All @@ -2021,22 +1960,6 @@ const mnemonic_code* instruction::find_mnemonic_codes(std::string_view name) noe
return nullptr;
return std::to_address(it);
}
#else
static_assert(std::is_sorted(std::begin(mnemonic_codes), std::end(mnemonic_codes), [](const auto& l, const auto& r) {
return l.name() < r.name();
}));

const mnemonic_code* instruction::find_mnemonic_codes(std::string_view name) noexcept
{
auto it =
std::lower_bound(std::begin(mnemonic_codes), std::end(mnemonic_codes), name, [](const auto& l, const auto& r) {
return l.name() < r;
});
if (it == std::end(mnemonic_codes) || it->name() != name)
return nullptr;
return std::to_address(it);
}
#endif

const mnemonic_code& instruction::get_mnemonic_codes(std::string_view name) noexcept
{
Expand Down
11 changes: 3 additions & 8 deletions parser_library/src/context/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,13 +522,8 @@ class machine_instruction
, m_instr_set_affiliation(instr_set_affiliation)
, m_format(format)
, m_reladdr_mask(generate_reladdr_bitmask(operands))
#ifdef __cpp_lib_ranges
, m_optional_op_count(
(unsigned char)std::ranges::count(operands, true, &checking::machine_operand_format::optional))
#else
, m_optional_op_count((unsigned char)std::count_if(
operands.begin(), operands.end(), [](const auto& op) { return op.optional; }))
#endif
(unsigned char)std::ranges::count_if(operands, &checking::machine_operand_format::optional))
, m_operand_len((unsigned char)operands.size())
, m_operands(operands.data())
, m_details(d)
Expand Down Expand Up @@ -729,8 +724,8 @@ class mnemonic_code
, m_name(name)
{
assert(transform.size() <= m_transform.size());
std::copy(transform.begin(), transform.end(), m_transform.begin());
const auto insert_count = std::count_if(transform.begin(), transform.end(), [](auto t) { return t.insert; });
std::ranges::copy(transform, m_transform.begin());
const auto insert_count = std::ranges::count_if(transform, [](auto t) { return t.insert; });
[[maybe_unused]] const auto total = std::accumulate(
transform.begin(), transform.end(), (size_t)0, [](size_t res, auto t) { return res + t.skip + t.insert; });
assert(total <= instr->operands().size());
Expand Down
17 changes: 5 additions & 12 deletions parser_library/src/context/ordinary_assembly/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
#include <algorithm>
#include <cassert>
#include <concepts>
#if __cpp_lib_polymorphic_allocator >= 201902L && __cpp_lib_memory_resource >= 201603L
# include <memory_resource>
#endif
#include <memory_resource>
#include <numeric>
#include <span>
#include <type_traits>
Expand Down Expand Up @@ -77,7 +75,7 @@ void space::resolve(space_ptr this_space, space_ptr value)

assert(this_space->kind == space_kind::LOCTR_UNKNOWN);

this_space->resolved_ptrs.push_back(address::space_entry(value, 1));
this_space->resolved_ptrs.emplace_back(std::move(value), 1);

this_space->resolved_ = true;
}
Expand Down Expand Up @@ -121,18 +119,13 @@ struct normalization_helper
normalization_helper(normalization_helper&&) = delete;
normalization_helper& operator=(const normalization_helper&) = delete;
normalization_helper& operator=(normalization_helper&&) = delete;
#if __cpp_lib_polymorphic_allocator >= 201902L && __cpp_lib_memory_resource >= 201603L
normalization_helper()
: buffer_resource(buffer.data(), buffer.size())
, map(&buffer_resource)
{}
alignas(std::max_align_t) std::array<unsigned char, 8 * 1024> buffer;
std::pmr::monotonic_buffer_resource buffer_resource;
std::pmr::unordered_map<space*, size_t> map;
#else
normalization_helper() = default;
std::unordered_map<space*, size_t> map;
#endif
};

int get_unresolved_spaces(std::span<const address::space_entry> spaces,
Expand Down Expand Up @@ -235,7 +228,7 @@ address::base_list merge_bases(const address::base_list& l, const address::base_
cnt *= -1;
return address::base_list(std::move(result));
}
if (std::equal(l.bases.begin(), l.bases.end(), r.bases.begin(), r.bases.end()))
if (std::ranges::equal(l.bases, r.bases))
return {};
}

Expand All @@ -246,7 +239,7 @@ address::base_list merge_bases(const address::base_list& l, const address::base_
for (auto& [b, cnt] : r.bases)
result->emplace_back(b, operation == merge_op::add ? cnt : -cnt);

std::sort(result->begin(), result->end(), [](const auto& l, const auto& r) { return l.first < r.first; });
std::ranges::sort(*result, {}, &address::base_entry::first);
utils::merge_unsorted(
*result,
l.bases,
Expand Down Expand Up @@ -287,7 +280,7 @@ std::pair<std::span<const address::space_entry>, std::span<const address::space_
auto common = std::min(l.size(), r.size());
return { l.subspan(common), r.subspan(common) };
}
auto [le, re] = std::mismatch(l.begin(), l.end(), r.begin(), r.end());
auto [le, re] = std::ranges::mismatch(l, r);
return { { le, l.end() }, { re, r.end() } };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,32 @@ dependency_collector& dependency_collector::operator/=(const dependency_collecto
namespace {
struct merge_spaces_comparator
{
auto operator()(const space_ptr& l, const address::space_entry& r) const { return l.get() <=> r.first.get(); }
auto operator()(const space_ptr& l, const space_ptr& r) const { return l.get() <=> r.get(); } // libc++
auto operator()(const space_ptr& l, const address::space_entry& r) const noexcept { return l <=> r.first; }
auto operator()(const space_ptr& l, const space_ptr& r) const noexcept { return l <=> r; }
};
struct merge_spaces
{
void operator()(space_ptr&, const address::space_entry&) const {}
auto operator()(const address::space_entry& e) const { return e.first; }
void operator()(space_ptr&, const address::space_entry&) const noexcept {}
auto operator()(const address::space_entry& e) const noexcept { return e.first; }
};
struct name_comparator
{
auto operator()(const id_index& l, const id_index& r) const { return l <=> r; }
auto operator()(const id_index& l, const symbolic_reference& r) const { return l <=> r.name; }
auto operator()(const symbolic_reference& l, const symbolic_reference& r) const { return l.name <=> r.name; }
auto operator()(const id_index& l, const id_index& r) const noexcept { return l <=> r; }
auto operator()(const id_index& l, const symbolic_reference& r) const noexcept { return l <=> r.name; }
auto operator()(const symbolic_reference& l, const symbolic_reference& r) const noexcept
{
return l.name <=> r.name;
}
};
struct name_merger
{
void operator()(id_index&, const symbolic_reference&) const {}
auto operator()(const symbolic_reference& e) const { return e.name; }
void operator()(id_index&, const symbolic_reference&) const noexcept {}
auto operator()(const symbolic_reference& e) const noexcept { return e.name; }
};
struct flags_merger
{
void operator()(symbolic_reference& l, const symbolic_reference& r) const { l.flags |= r.flags; }
auto operator()(const symbolic_reference& e) const { return e; }
void operator()(symbolic_reference& l, const symbolic_reference& r) const noexcept { l.flags |= r.flags; }
auto operator()(const symbolic_reference& e) const noexcept { return e; }
};
} // namespace

Expand Down Expand Up @@ -155,12 +158,9 @@ bool dependency_collector::merge_undef(const dependency_collector& holder)

utils::merge_sorted(undefined_symbolics, holder.undefined_symbolics, name_comparator(), flags_merger());

utils::merge_sorted(unresolved_spaces, holder.unresolved_spaces, [](const auto& l, const auto& r) {
return l.get() <=> r.get(); // libc++
});
utils::merge_sorted(unresolved_spaces, holder.unresolved_spaces, std::compare_three_way());

return has_error
|| std::any_of(undefined_symbolics.begin(), undefined_symbolics.end(), [](const auto& e) { return e.get(); });
return has_error || std::ranges::any_of(undefined_symbolics, [](const auto& e) { return e.get(); });
}

void dependency_collector::add_sub(const dependency_collector& holder, bool add)
Expand Down
Loading

0 comments on commit 08b4313

Please sign in to comment.