Skip to content

Commit

Permalink
refactor: SET_t structure cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Jan 31, 2024
1 parent 13f3686 commit 652f507
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 120 deletions.
55 changes: 3 additions & 52 deletions parser_library/src/context/common_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,68 +15,19 @@
#include "common_types.h"

#include <cassert>
#include <cctype>

namespace hlasm_plugin::parser_library::context {

SET_t::SET_t(context::A_t value)
: a_value(value)
, b_value(value)
, c_value(object_traits<C_t>::default_v())
, type(SET_t_enum::A_TYPE)
{}

SET_t::SET_t(context::B_t value)
: a_value(value)
, b_value(value)
, c_value(object_traits<C_t>::default_v())
, type(SET_t_enum::B_TYPE)
{}

SET_t::SET_t(context::C_t value)
: a_value(object_traits<A_t>::default_v())
, b_value(object_traits<B_t>::default_v())
, c_value(std::move(value))
, type(SET_t_enum::C_TYPE)
{}

SET_t::SET_t(const char* value)
: a_value(object_traits<A_t>::default_v())
, b_value(object_traits<B_t>::default_v())
, c_value(value)
, type(SET_t_enum::C_TYPE)
{}

SET_t::SET_t(SET_t_enum type)
: a_value(object_traits<A_t>::default_v())
, b_value(object_traits<B_t>::default_v())
, c_value(object_traits<C_t>::default_v())
, type(type)
{}

A_t& SET_t::access_a() { return a_value; }

B_t& SET_t::access_b() { return b_value; }

C_t& SET_t::access_c() { return c_value; }

const A_t& SET_t::access_a() const { return a_value; }

const B_t& SET_t::access_b() const { return b_value; }

const C_t& SET_t::access_c() const { return c_value; }

bool SET_t::operator==(const SET_t& r) const noexcept
{
if (type != r.type)
if (value_type != r.value_type)
return false;

switch (type)
switch (value_type)
{
case SET_t_enum::A_TYPE:
return a_value == r.a_value;
case SET_t_enum::B_TYPE:
return b_value == r.b_value;
return a_value == r.a_value;
case SET_t_enum::C_TYPE:
return c_value == r.c_value;
case SET_t_enum::UNDEF_TYPE:
Expand Down
70 changes: 36 additions & 34 deletions parser_library/src/context/common_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,60 +61,62 @@ template<>
struct object_traits<A_t>
{
static constexpr SET_t_enum type_enum = SET_t_enum::A_TYPE;
static const A_t& default_v()
{
static A_t def = 0;
return def;
}
static constexpr A_t default_v() noexcept { return 0; }
};

template<>
struct object_traits<B_t>
{
static constexpr SET_t_enum type_enum = SET_t_enum::B_TYPE;
static const B_t& default_v()
{
static B_t def = false;
return def;
}
static constexpr B_t default_v() noexcept { return false; }
};

template<>
struct object_traits<C_t>
{
static constexpr SET_t_enum type_enum = SET_t_enum::C_TYPE;
static const C_t& default_v()
{
static C_t def("");
return def;
}
static C_t default_v() noexcept { return C_t(); } // llvm-14 - constexpr
};

// struct aggregating SET types for easier usage
struct SET_t
{
private:
A_t a_value;
B_t b_value;
C_t c_value;
C_t c_value = {};
A_t a_value = 0;
SET_t_enum value_type;

public:
SET_t(A_t value);
SET_t(B_t value);
SET_t(C_t value);
// for string literals (otherwise they prefer conversion to bool rather than to string)
SET_t(const char* value);
SET_t(SET_t_enum type = SET_t_enum::UNDEF_TYPE);

SET_t_enum type;

A_t& access_a();
B_t& access_b();
C_t& access_c();

const A_t& access_a() const;
const B_t& access_b() const;
const C_t& access_c() const;
SET_t(A_t value) noexcept
: a_value(value)
, value_type(SET_t_enum::A_TYPE)
{}

SET_t(B_t value) noexcept
: a_value(static_cast<A_t>(value))
, value_type(SET_t_enum::B_TYPE)
{}

SET_t(C_t value) noexcept
: c_value(std::move(value))
, value_type(SET_t_enum::C_TYPE)
{}

SET_t(const char* value)
: c_value(value)
, value_type(SET_t_enum::C_TYPE)
{}

SET_t(SET_t_enum type = SET_t_enum::UNDEF_TYPE) noexcept
: value_type(type)
{}

SET_t_enum type() const noexcept { return value_type; }

A_t access_a() const noexcept { return a_value; }
B_t access_b() const noexcept { return !!a_value; }
C_t& access_c() noexcept { return c_value; }
const C_t& access_c() const noexcept { return c_value; }

bool operator==(const SET_t& r) const noexcept;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ ca_expression::ca_expression(context::SET_t_enum expr_kind, range expr_range)
context::SET_t ca_expression::convert_return_types(
context::SET_t retval, context::SET_t_enum type, const evaluation_context&) const
{
if (type != retval.type)
if (type != retval.type())
{
if (retval.type == context::SET_t_enum::A_TYPE && type == context::SET_t_enum::B_TYPE)
if (retval.type() == context::SET_t_enum::A_TYPE && type == context::SET_t_enum::B_TYPE)
return retval.access_a() != 0;
if (retval.type == context::SET_t_enum::B_TYPE && type == context::SET_t_enum::A_TYPE)
if (retval.type() == context::SET_t_enum::B_TYPE && type == context::SET_t_enum::A_TYPE)
return (context::A_t)retval.access_b();

return context::SET_t(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ std::optional<bool> t_attr_special_case(std::vector<context::id_index>& symbols,
if (result)
return result;

if (auto v = o_string->evaluate(eval_ctx); v.type != context::SET_t_enum::C_TYPE || v.access_c() != "O")
if (auto v = o_string->evaluate(eval_ctx); v.type() != context::SET_t_enum::C_TYPE || v.access_c() != "O")
return std::nullopt;

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ context::SET_t ca_function::B2C(const context::C_t& param, diagnostic_adder& add
context::SET_t ca_function::B2D(const context::C_t& param, diagnostic_adder& add_diagnostic)
{
auto tmp = B2A(param, add_diagnostic);
if (tmp.type == context::SET_t_enum::UNDEF_TYPE)
if (tmp.type() == context::SET_t_enum::UNDEF_TYPE)
return tmp;
return A2D(tmp.access_a());
}
Expand Down Expand Up @@ -516,7 +516,7 @@ context::SET_t ca_function::C2B(const context::C_t& param, diagnostic_adder& add
context::SET_t ca_function::C2D(const context::C_t& param, diagnostic_adder& add_diagnostic)
{
auto tmp = C2A(param, add_diagnostic);
if (tmp.type == context::SET_t_enum::UNDEF_TYPE)
if (tmp.type() == context::SET_t_enum::UNDEF_TYPE)
return tmp;
return A2D(tmp.access_a());
}
Expand Down Expand Up @@ -553,7 +553,7 @@ context::SET_t ca_function::D2B(const context::C_t& param, diagnostic_adder& add
return "";

auto tmp = D2A(param, add_diagnostic);
if (tmp.type == context::SET_t_enum::UNDEF_TYPE)
if (tmp.type() == context::SET_t_enum::UNDEF_TYPE)
return tmp;
return A2B(tmp.access_a());
}
Expand All @@ -564,7 +564,7 @@ context::SET_t ca_function::D2C(const context::C_t& param, diagnostic_adder& add
RET_ERRPARM;

auto tmp = D2A(param, add_diagnostic);
if (tmp.type == context::SET_t_enum::UNDEF_TYPE)
if (tmp.type() == context::SET_t_enum::UNDEF_TYPE)
return tmp;
return A2C(tmp.access_a());
}
Expand All @@ -575,7 +575,7 @@ context::SET_t ca_function::D2X(const context::C_t& param, diagnostic_adder& add
RET_ERRPARM;

auto tmp = D2A(param, add_diagnostic);
if (tmp.type == context::SET_t_enum::UNDEF_TYPE)
if (tmp.type() == context::SET_t_enum::UNDEF_TYPE)
return tmp;
return A2X(tmp.access_a());
}
Expand Down Expand Up @@ -697,7 +697,7 @@ context::SET_t ca_function::X2C(const context::C_t& param, diagnostic_adder& add
context::SET_t ca_function::X2D(const context::C_t& param, diagnostic_adder& add_diagnostic)
{
auto tmp = X2A(param, add_diagnostic);
if (tmp.type == context::SET_t_enum::UNDEF_TYPE)
if (tmp.type() == context::SET_t_enum::UNDEF_TYPE)
return tmp;
return A2D(tmp.access_a());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ bool ca_symbol_attribute::get_undefined_attributed_symbols(
{
context::SET_t substituted_name = vs->evaluate(eval_ctx);

if (substituted_name.type != context::SET_t_enum::C_TYPE)
if (substituted_name.type() != context::SET_t_enum::C_TYPE)
return false;

auto [valid, ord_name] =
Expand Down Expand Up @@ -418,7 +418,7 @@ context::SET_t ca_symbol_attribute::evaluate_substituted(context::id_index var_n
context::SET_t substituted_name =
get_var_sym_value(eval_ctx.hlasm_ctx, var_name, expr_subscript, var_range, eval_ctx.diags);

if (substituted_name.type != context::SET_t_enum::C_TYPE)
if (substituted_name.type() != context::SET_t_enum::C_TYPE)
{
if (attribute != context::data_attr_kind::O && attribute != context::data_attr_kind::T)
eval_ctx.diags.add_diagnostic(diagnostic_op::error_E066(expr_range));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ context::SET_t ca_var_sym::evaluate(const evaluation_context& eval_ctx) const
context::SET_t ca_var_sym::convert_return_types(
context::SET_t retval, context::SET_t_enum type, const evaluation_context& eval_ctx) const
{
if (retval.type == context::SET_t_enum::C_TYPE)
if (retval.type() == context::SET_t_enum::C_TYPE)
{
diagnostic_adder add_diags(eval_ctx.diags, expr_range);
switch (type)
Expand All @@ -96,11 +96,11 @@ context::SET_t ca_var_sym::convert_return_types(
return context::SET_t(expr_kind);
}
}
else if (retval.type == context::SET_t_enum::B_TYPE && type == context::SET_t_enum::A_TYPE)
else if (retval.type() == context::SET_t_enum::B_TYPE && type == context::SET_t_enum::A_TYPE)
{
retval = context::SET_t(retval.access_b() ? 1 : 0);
}
else if (retval.type == context::SET_t_enum::A_TYPE && type == context::SET_t_enum::B_TYPE)
else if (retval.type() == context::SET_t_enum::A_TYPE && type == context::SET_t_enum::B_TYPE)
{
retval = context::SET_t(!!retval.access_a());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void ca_processor::process_MHELP(const semantics::complete_statement& stmt)
else if (ca_op->kind == semantics::ca_kind::VAR)
{
auto val = ca_op->access_var()->variable_symbol->evaluate(eval_ctx);
if (val.type == context::SET_t_enum::A_TYPE)
if (val.type() == context::SET_t_enum::A_TYPE)
value = val.access_a();
}
else
Expand Down
2 changes: 1 addition & 1 deletion parser_library/src/semantics/concatenation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void var_sym_conc::resolve(diagnostic_op_consumer& diag) const { symbol->resolve

std::string var_sym_conc::evaluate(context::SET_t varsym_value)
{
switch (varsym_value.type)
switch (varsym_value.type())
{
case context::SET_t_enum::A_TYPE:
return std::to_string(std::abs(varsym_value.access_a()));
Expand Down
6 changes: 3 additions & 3 deletions parser_library/test/expressions/ca_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ std::ostream& operator<<(std::ostream& os, const func_test_param& param)
{
for (auto& p : param.params)
{
if (p.type == context::SET_t_enum::A_TYPE)
if (p.type() == context::SET_t_enum::A_TYPE)
os << p.access_a();
else if (p.type == context::SET_t_enum::B_TYPE)
else if (p.type() == context::SET_t_enum::B_TYPE)
os << p.access_b();
else if (p.type == context::SET_t_enum::C_TYPE)
else if (p.type() == context::SET_t_enum::C_TYPE)
os << p.access_c();
os << " ";
}
Expand Down
20 changes: 10 additions & 10 deletions parser_library/test/expressions/ca_operator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ std::ostream& operator<<(std::ostream& os, const op_test_param& param)
{
for (auto& p : param.params)
{
if (p.type == context::SET_t_enum::A_TYPE)
if (p.type() == context::SET_t_enum::A_TYPE)
os << p.access_a();
else if (p.type == context::SET_t_enum::B_TYPE)
else if (p.type() == context::SET_t_enum::B_TYPE)
os << p.access_b();
else if (p.type == context::SET_t_enum::C_TYPE)
else if (p.type() == context::SET_t_enum::C_TYPE)
os << p.access_c();
os << " ";
}
Expand All @@ -70,21 +70,21 @@ class ca_op : public ::testing::TestWithParam<op_test_param>
if (GetParam().operation == ca_expr_ops::NOT || GetParam().kind == SET_t_enum::C_TYPE)
{
ca_function_unary_operator op(
nullptr, GetParam().operation, GetParam().kind, range(), GetParam().params[0].type);
nullptr, GetParam().operation, GetParam().kind, range(), GetParam().params[0].type());

return op.operation(GetParam().params[0], eval_ctx);
}
else
{
ca_expr_ptr left;
if (GetParam().params[0].type == SET_t_enum::A_TYPE)
if (GetParam().params[0].type() == SET_t_enum::A_TYPE)
left = std::make_unique<ca_constant>(1, range());
else
left =
std::make_unique<ca_string>(semantics::concat_chain {}, nullptr, ca_string::substring_t(), range());

ca_function_binary_operator op(
std::move(left), nullptr, GetParam().operation, GetParam().kind, range(), GetParam().params[0].type);
std::move(left), nullptr, GetParam().operation, GetParam().kind, range(), GetParam().params[0].type());

return op.operation(GetParam().params[0], GetParam().params[1], eval_ctx);
}
Expand Down Expand Up @@ -159,13 +159,13 @@ TEST_P(ca_op, test)
{
auto result = get_result();

ASSERT_EQ(result.type, GetParam().true_result.type);
ASSERT_EQ(result.type(), GetParam().true_result.type());

if (result.type == SET_t_enum::A_TYPE)
if (result.type() == SET_t_enum::A_TYPE)
EXPECT_EQ(result.access_a(), GetParam().true_result.access_a());
else if (result.type == SET_t_enum::B_TYPE)
else if (result.type() == SET_t_enum::B_TYPE)
EXPECT_EQ(result.access_b(), GetParam().true_result.access_b());
else if (result.type == SET_t_enum::C_TYPE)
else if (result.type() == SET_t_enum::C_TYPE)
EXPECT_EQ(result.access_c(), GetParam().true_result.access_c());
else
FAIL();
Expand Down
8 changes: 4 additions & 4 deletions parser_library/test/expressions/ca_symbol_attribute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ TEST_P(ca_attr, test)

auto result = create_var_sym_attr(GetParam().attr, name).evaluate(eval_ctx);

ASSERT_EQ(result.type, GetParam().result.type);
ASSERT_EQ(result.type(), GetParam().result.type());

if (result.type == context::SET_t_enum::A_TYPE)
if (result.type() == context::SET_t_enum::A_TYPE)
EXPECT_EQ(result.access_a(), GetParam().result.access_a());
else if (result.type == context::SET_t_enum::B_TYPE)
else if (result.type() == context::SET_t_enum::B_TYPE)
EXPECT_EQ(result.access_b(), GetParam().result.access_b());
else if (result.type == context::SET_t_enum::C_TYPE)
else if (result.type() == context::SET_t_enum::C_TYPE)
EXPECT_EQ(result.access_c(), GetParam().result.access_c());
else
FAIL();
Expand Down

0 comments on commit 652f507

Please sign in to comment.