From 5f54f6b859270bcd8722090443a02f6c9c5fd871 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Sun, 29 Oct 2023 11:52:27 +0100 Subject: [PATCH] fix: Implemented is_floating_point type trait --- src/parser/cxx/parser.cc | 21 +++++++++++++++++++++ tests/unit_tests/sema/type_traits_01.cc | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/parser/cxx/parser.cc b/src/parser/cxx/parser.cc index 908715a3..8fea00d3 100644 --- a/src/parser/cxx/parser.cc +++ b/src/parser/cxx/parser.cc @@ -246,6 +246,22 @@ struct Parser::TypeTraits { } is_integral; + struct { + constexpr auto operator()(const FloatType*) const -> bool { return true; } + + constexpr auto operator()(const DoubleType*) const -> bool { return true; } + + constexpr auto operator()(const LongDoubleType*) const -> bool { + return true; + } + + constexpr auto operator()(const QualType* type) const -> bool { + return visit(*this, type->elementType()); + } + + constexpr auto operator()(auto) const -> bool { return false; } + } is_floating_point; + struct { constexpr auto operator()(const QualType* type) const -> bool { return type->isConst(); @@ -2571,6 +2587,11 @@ auto Parser::parse_builtin_call_expression(ExpressionAST*& yyast) -> bool { break; } + case TokenKind::T___IS_FLOATING_POINT: { + ast->constValue = visit(TypeTraits{*this}.is_floating_point, firstType); + break; + } + case TokenKind::T___IS_CONST: { ast->constValue = visit(TypeTraits{*this}.is_const, firstType); break; diff --git a/tests/unit_tests/sema/type_traits_01.cc b/tests/unit_tests/sema/type_traits_01.cc index 9b33dbd3..c88f513b 100644 --- a/tests/unit_tests/sema/type_traits_01.cc +++ b/tests/unit_tests/sema/type_traits_01.cc @@ -51,6 +51,23 @@ static_assert(__is_integral(void)); // expected-error@1 {{static assert failed}} static_assert(__is_integral(void*)); +// +// is_floating_point trait +// + +static_assert(__is_floating_point(float)); +static_assert(__is_floating_point(double)); +static_assert(__is_floating_point(long double)); +static_assert(__is_floating_point(const float)); +static_assert(__is_floating_point(const double)); +static_assert(__is_floating_point(const long double)); + +// expected-error@1 {{static assert failed}} +static_assert(__is_floating_point(bool)); + +// expected-error@1 {{static assert failed}} +static_assert(__is_floating_point(float*)); + // // is_const trait //