Skip to content

Commit

Permalink
fix: Implemented is_floating_point type trait
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Oct 29, 2023
1 parent d1c463d commit 5f54f6b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/unit_tests/sema/type_traits_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down

0 comments on commit 5f54f6b

Please sign in to comment.