Skip to content

Commit

Permalink
fix: Implemented __is_integral type trait
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoraggi committed Oct 29, 2023
1 parent 845f336 commit d1c463d
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,65 @@ struct Parser::TypeTraits {
constexpr auto operator()(auto) const -> bool { return false; }
} is_void;

struct {
constexpr auto operator()(const BoolType*) const -> bool { return true; }

constexpr auto operator()(const SignedCharType*) const -> bool {
return true;
}

constexpr auto operator()(const ShortIntType*) const -> bool {
return true;
}

constexpr auto operator()(const IntType*) const -> bool { return true; }

constexpr auto operator()(const LongIntType*) const -> bool { return true; }

constexpr auto operator()(const LongLongIntType*) const -> bool {
return true;
}

constexpr auto operator()(const UnsignedCharType*) const -> bool {
return true;
}

constexpr auto operator()(const UnsignedShortIntType*) const -> bool {
return true;
}

constexpr auto operator()(const UnsignedIntType*) const -> bool {
return true;
}

constexpr auto operator()(const UnsignedLongIntType*) const -> bool {
return true;
}

constexpr auto operator()(const UnsignedLongLongIntType*) const -> bool {
return true;
}

constexpr auto operator()(const CharType*) const -> bool { return true; }

constexpr auto operator()(const Char8Type*) const -> bool { return true; }

constexpr auto operator()(const Char16Type*) const -> bool { return true; }

constexpr auto operator()(const Char32Type*) const -> bool { return true; }

constexpr auto operator()(const WideCharType*) 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_integral;

struct {
constexpr auto operator()(const QualType* type) const -> bool {
return type->isConst();
Expand Down Expand Up @@ -2507,6 +2566,11 @@ auto Parser::parse_builtin_call_expression(ExpressionAST*& yyast) -> bool {
break;
}

case TokenKind::T___IS_INTEGRAL: {
ast->constValue = visit(TypeTraits{*this}.is_integral, firstType);
break;
}

case TokenKind::T___IS_CONST: {
ast->constValue = visit(TypeTraits{*this}.is_const, firstType);
break;
Expand Down
36 changes: 36 additions & 0 deletions tests/unit_tests/sema/type_traits_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ static_assert(__is_void(int));
// expected-error@1 {{static assert failed}}
static_assert(__is_void(void*));

//
// is_integral trait
//

static_assert(__is_integral(bool));
static_assert(__is_integral(char));
static_assert(__is_integral(signed char));
static_assert(__is_integral(unsigned char));
static_assert(__is_integral(char8_t));
static_assert(__is_integral(char16_t));
static_assert(__is_integral(char32_t));
static_assert(__is_integral(wchar_t));
static_assert(__is_integral(short));
static_assert(__is_integral(unsigned short));
static_assert(__is_integral(int));
static_assert(__is_integral(unsigned int));
static_assert(__is_integral(long));
static_assert(__is_integral(unsigned long));
static_assert(__is_integral(long long));
static_assert(__is_integral(unsigned long long));

// expected-error@1 {{static assert failed}}
static_assert(__is_integral(float));

// expected-error@1 {{static assert failed}}
static_assert(__is_integral(double));

// expected-error@1 {{static assert failed}}
static_assert(__is_integral(long double));

// expected-error@1 {{static assert failed}}
static_assert(__is_integral(void));

// expected-error@1 {{static assert failed}}
static_assert(__is_integral(void*));

//
// is_const trait
//
Expand Down

0 comments on commit d1c463d

Please sign in to comment.