Skip to content

Commit

Permalink
refactor: remove about widening conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashigeru committed Aug 8, 2024
1 parent c82c2c3 commit 37268ca
Show file tree
Hide file tree
Showing 10 changed files with 1 addition and 280 deletions.
8 changes: 0 additions & 8 deletions include/yugawara/aggregate/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,6 @@ class declaration {
*/
declaration& incremental(bool enabled) noexcept;

/**
* @brief returns whether or not this function has wider parameters than the given one.
* @param other the target function declaration
* @return true if each parameter is wider than the target function
* @return false otherwise
*/
[[nodiscard]] bool has_wider_parameters(declaration const& other) const noexcept;

/**
* @brief appends string representation of the given value.
* @param out the target output
Expand Down
8 changes: 0 additions & 8 deletions include/yugawara/function/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,6 @@ class declaration {
/// @copydoc parameter_types()
[[nodiscard]] std::vector<type_pointer> const& shared_parameter_types() const noexcept;

/**
* @brief returns whether or not this function has wider parameters than the given one.
* @param other the target function declaration
* @return true if each parameter is wider than the target function
* @return false otherwise
*/
[[nodiscard]] bool has_wider_parameters(declaration const& other) const noexcept;

/**
* @brief appends string representation of the given value.
* @param out the target output
Expand Down
10 changes: 0 additions & 10 deletions include/yugawara/type/conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,6 @@ util::ternary is_assignment_convertible(::takatori::type::data const& type, ::ta
*/
util::ternary is_cast_convertible(::takatori::type::data const& type, ::takatori::type::data const& target) noexcept;

/**
* @brief returns whether or not the "widening conversion" is applicable to the given types.
* @param type the type to be converted
* @param target the conversion target type
* @return yes if the conversion is applicable
* @return no if the conversion is not applicable
* @return unknown if the parameters contain an erroneous or pending type
*/
util::ternary is_widening_convertible(::takatori::type::data const& type, ::takatori::type::data const& target) noexcept;

/**
* @brief returns whether or not the type is the most upper-bound compatible type in its category.
* @param type the target type
Expand Down
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ add_library(yugawara

# function information
yugawara/function/declaration.cpp
yugawara/function/utils.cpp

# aggregate function information
# aggregate function information
yugawara/aggregate/declaration.cpp

# descriptor entities
Expand Down
6 changes: 0 additions & 6 deletions src/yugawara/aggregate/declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <takatori/util/vector_print_support.h>
#include <takatori/util/clonable.h>

#include <yugawara/function/utils.h>

namespace yugawara::aggregate {

declaration::declaration(
Expand Down Expand Up @@ -103,10 +101,6 @@ declaration& declaration::incremental(bool enabled) noexcept {
return *this;
}

bool declaration::has_wider_parameters(declaration const& other) const noexcept {
return function::utils::each_is_widening_convertible(other.parameter_types_, parameter_types_);
}

std::ostream& operator<<(std::ostream& out, declaration const& value) {
return out << "aggregate_function("
<< "definition_id=" << value.definition_id() << ", "
Expand Down
7 changes: 0 additions & 7 deletions src/yugawara/function/declaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include <takatori/util/clonable.h>
#include <yugawara/schema/declaration.h>


#include "utils.h"

namespace yugawara::function {

declaration::declaration(
Expand Down Expand Up @@ -92,10 +89,6 @@ std::vector<declaration::type_pointer> const& declaration::shared_parameter_type
return parameter_types_;
}

bool declaration::has_wider_parameters(declaration const& other) const noexcept {
return utils::each_is_widening_convertible(other.parameter_types_, parameter_types_);
}

std::ostream& operator<<(std::ostream& out, declaration const& value) {
return out << "function("
<< "definition_id=" << value.definition_id() << ", "
Expand Down
23 changes: 0 additions & 23 deletions src/yugawara/function/utils.cpp

This file was deleted.

14 changes: 0 additions & 14 deletions src/yugawara/function/utils.h

This file was deleted.

122 changes: 0 additions & 122 deletions src/yugawara/type/conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,128 +748,6 @@ static constexpr int compare_flexible(std::optional<std::size_t> left, std::opti
return *left < *right ? -1 : +1;
}

// NOLINTNEXTLINE(readability-function-cognitive-complexity)
util::ternary is_widening_convertible(takatori::type::data const& type, takatori::type::data const& target) noexcept {
if (is_conversion_stop_type(type) || is_conversion_stop_type(target)) return ternary::unknown;

// can convert unknown to anything
if (type.kind() == kind::unknown) {
return ternary::yes;
}

switch (npair(type.kind(), target.kind())) {
case npair(kind::boolean, kind::boolean):

case npair(kind::int1, kind::int1):
case npair(kind::int1, kind::int2):
case npair(kind::int1, kind::int4):
case npair(kind::int1, kind::int8):
case npair(kind::int1, kind::float4):
case npair(kind::int1, kind::float8):

case npair(kind::int2, kind::int2):
case npair(kind::int2, kind::int4):
case npair(kind::int2, kind::int8):
case npair(kind::int2, kind::float4):
case npair(kind::int2, kind::float8):

case npair(kind::int4, kind::int4):
case npair(kind::int4, kind::int8):
case npair(kind::int4, kind::float4):
case npair(kind::int4, kind::float8):

case npair(kind::int8, kind::int8):
case npair(kind::int8, kind::float4):
case npair(kind::int8, kind::float8):

case npair(kind::decimal, kind::float4):
case npair(kind::decimal, kind::float8):

case npair(kind::float4, kind::float4):
case npair(kind::float4, kind::float8):

case npair(kind::float8, kind::float8):

case npair(kind::date, kind::date):
case npair(kind::date, kind::time_point):

case npair(kind::time_of_day, kind::time_of_day):
case npair(kind::time_of_day, kind::time_point):

case npair(kind::time_point, kind::time_point):

case npair(kind::datetime_interval, kind::datetime_interval):
return ternary::yes;

case npair(kind::int1, kind::decimal):
return is_widening_convertible(model::decimal { decimal_precision_int1, 0 }, target);
case npair(kind::int2, kind::decimal):
return is_widening_convertible(model::decimal { decimal_precision_int2, 0 }, target);
case npair(kind::int4, kind::decimal):
return is_widening_convertible(model::decimal { decimal_precision_int4, 0 }, target);
case npair(kind::int8, kind::decimal):
return is_widening_convertible(model::decimal { decimal_precision_int8, 0 }, target);

case npair(kind::decimal, kind::int1):
return is_widening_convertible(type, model::decimal { decimal_precision_int1 - 1, 0 });
case npair(kind::decimal, kind::int2):
return is_widening_convertible(type, model::decimal { decimal_precision_int2 - 1, 0 });
case npair(kind::decimal, kind::int4):
return is_widening_convertible(type, model::decimal { decimal_precision_int4 - 1, 0 });
case npair(kind::decimal, kind::int8):
return is_widening_convertible(type, model::decimal { decimal_precision_int8 - 1, 0 });

case npair(kind::decimal, kind::decimal): {
auto&& a = unsafe_downcast<model::decimal>(type);
auto&& b = unsafe_downcast<model::decimal>(target);
if (compare_flexible(a.scale(), b.scale()) > 0) return ternary::no;
if (!b.precision()) return ternary::yes;
if (!a.precision()) return ternary::no;
auto ai = static_cast<std::int64_t>(*a.precision()) - a.scale().value_or(0);
auto bi = static_cast<std::int64_t>(*b.precision()) - b.scale().value_or(0);
return ternary_of(ai <= bi);
}

case npair(kind::character, kind::character): {
auto&& a = unsafe_downcast<model::character>(type);
auto&& b = unsafe_downcast<model::character>(target);
if (a.varying() && !b.varying()) return ternary::no;
if (!a.length()) return ternary::no;
if (!b.length()) return ternary::yes;
return ternary_of(*a.length() <= b.length());
}

case npair(kind::octet, kind::octet): {
auto&& a = unsafe_downcast<model::octet>(type);
auto&& b = unsafe_downcast<model::octet>(target);
if (a.varying() && !b.varying()) return ternary::no;
if (!a.length()) return ternary::no;
if (!b.length()) return ternary::yes;
return ternary_of(*a.length() <= b.length());
}

case npair(kind::bit, kind::bit): {
auto&& a = unsafe_downcast<model::bit>(type);
auto&& b = unsafe_downcast<model::bit>(target);
if (a.varying() && !b.varying()) return ternary::no;
if (!a.length()) return ternary::no;
if (!b.length()) return ternary::yes;
return ternary_of(*a.length() <= b.length());
}

case npair(kind::array, kind::array):
case npair(kind::record, kind::record):
case npair(kind::declared, kind::declared):
return ternary_of(type == target);

case npair(kind::extension, kind::extension):
return ternary_of(type == target);

default:
return ternary::no;
}
}

util::ternary is_most_upperbound_compatible_type(const takatori::type::data &type) noexcept {
if (is_conversion_stop_type(type)) return ternary::unknown;

Expand Down
80 changes: 0 additions & 80 deletions test/yugawara/function/function_declaration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,86 +28,6 @@ TEST_F(function_declaration_test, simple) {
EXPECT_EQ(d.parameter_types()[0], t::int8());
}

TEST_F(function_declaration_test, has_wider_parameters_empty) {
declaration a {
1,
"f",
t::int4(),
{},
};
declaration b {
1,
"f",
t::int4(),
{},
};
EXPECT_TRUE(a.has_wider_parameters(b));
EXPECT_TRUE(b.has_wider_parameters(a));
}

TEST_F(function_declaration_test, has_wider_parameters_eq) {
declaration a {
1,
"f",
t::int4(),
{
t::int4 {},
},
};
declaration b {
1,
"f",
t::int4(),
{
t::int4 {},
},
};
EXPECT_TRUE(a.has_wider_parameters(b));
EXPECT_TRUE(b.has_wider_parameters(a));
}

TEST_F(function_declaration_test, has_wider_parameters_wider) {
declaration a {
1,
"f",
t::int4(),
{
t::int8 {},
},
};
declaration b {
1,
"f",
t::int4(),
{
t::int4 {},
},
};
EXPECT_TRUE(a.has_wider_parameters(b));
EXPECT_FALSE(b.has_wider_parameters(a));
}

TEST_F(function_declaration_test, has_wider_parameters_disjoint) {
declaration a {
1,
"f",
t::int4(),
{
t::boolean {},
},
};
declaration b {
1,
"f",
t::int4(),
{
t::int4 {},
},
};
EXPECT_FALSE(a.has_wider_parameters(b));
EXPECT_FALSE(b.has_wider_parameters(a));
}

TEST_F(function_declaration_test, output) {
declaration d {
1,
Expand Down

0 comments on commit 37268ca

Please sign in to comment.