public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ken Matsui <kmatsui@gcc.gnu.org>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Ken Matsui <kmatsui@gcc.gnu.org>
Subject: [PATCH 01/14] c++: Implement __is_integral built-in trait
Date: Wed, 10 Jan 2024 01:22:52 -0800	[thread overview]
Message-ID: <20240110194031.2384005-2-kmatsui@gcc.gnu.org> (raw)
In-Reply-To: <20240110194031.2384005-1-kmatsui@gcc.gnu.org>

This patch implements built-in trait for std::is_integral.

gcc/cp/ChangeLog:

	* cp-trait.def: Define __is_integral.
	* constraint.cc (diagnose_trait_expr): Handle
	CPTK_IS_INTEGRAL.
	* semantics.cc (trait_expr_value): Likewise.
	(finish_trait_expr): Likewise.
	(integral_type_p): New function.

gcc/testsuite/ChangeLog:

	* g++.dg/ext/has-builtin-1.C: Test existence of __is_integral.
	* g++.dg/ext/is_integral.C: New test.

Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
---
 gcc/cp/constraint.cc                     |  3 ++
 gcc/cp/cp-trait.def                      |  1 +
 gcc/cp/semantics.cc                      | 18 +++++++++
 gcc/testsuite/g++.dg/ext/has-builtin-1.C |  3 ++
 gcc/testsuite/g++.dg/ext/is_integral.C   | 49 ++++++++++++++++++++++++
 5 files changed, 74 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/ext/is_integral.C

diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc
index fef68cf7ab2..3a105a2ee2a 100644
--- a/gcc/cp/constraint.cc
+++ b/gcc/cp/constraint.cc
@@ -3755,6 +3755,9 @@ diagnose_trait_expr (tree expr, tree args)
     case CPTK_IS_FUNCTION:
       inform (loc, "  %qT is not a function", t1);
       break;
+    case CPTK_IS_INTEGRAL:
+      inform (loc, "  %qT is not an integral type", t1);
+      break;
     case CPTK_IS_LAYOUT_COMPATIBLE:
       inform (loc, "  %qT is not layout compatible with %qT", t1, t2);
       break;
diff --git a/gcc/cp/cp-trait.def b/gcc/cp/cp-trait.def
index 394f006f20f..2773c3fa10e 100644
--- a/gcc/cp/cp-trait.def
+++ b/gcc/cp/cp-trait.def
@@ -70,6 +70,7 @@ DEFTRAIT_EXPR (IS_EMPTY, "__is_empty", 1)
 DEFTRAIT_EXPR (IS_ENUM, "__is_enum", 1)
 DEFTRAIT_EXPR (IS_FINAL, "__is_final", 1)
 DEFTRAIT_EXPR (IS_FUNCTION, "__is_function", 1)
+DEFTRAIT_EXPR (IS_INTEGRAL, "__is_integral", 1)
 DEFTRAIT_EXPR (IS_LAYOUT_COMPATIBLE, "__is_layout_compatible", 2)
 DEFTRAIT_EXPR (IS_LITERAL_TYPE, "__is_literal_type", 1)
 DEFTRAIT_EXPR (IS_MEMBER_FUNCTION_POINTER, "__is_member_function_pointer", 1)
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 3299e270446..1a335f69826 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -12258,6 +12258,20 @@ is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
   return ret;
 }
 
+/* Return true if T is an integral type.  With __STRICT_ANSI__, __int128 and
+   unsigned __int128 are not integral types.  */
+
+static bool
+integral_type_p (const_tree t)
+{
+  if (flag_iso)
+    return CP_INTEGRAL_TYPE_P (t)
+      && t != intTI_type_node
+      && t != unsigned_intTI_type_node;
+  else
+    return CP_INTEGRAL_TYPE_P (t);
+}
+
 /* Fold __builtin_is_corresponding_member call.  */
 
 tree
@@ -12465,6 +12479,9 @@ trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_FUNCTION:
       return type_code1 == FUNCTION_TYPE;
 
+    case CPTK_IS_INTEGRAL:
+      return integral_type_p (type1);
+
     case CPTK_IS_LAYOUT_COMPATIBLE:
       return layout_compatible_type_p (type1, type2);
 
@@ -12691,6 +12708,7 @@ finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
     case CPTK_IS_CLASS:
     case CPTK_IS_ENUM:
     case CPTK_IS_FUNCTION:
+    case CPTK_IS_INTEGRAL:
     case CPTK_IS_MEMBER_FUNCTION_POINTER:
     case CPTK_IS_MEMBER_OBJECT_POINTER:
     case CPTK_IS_MEMBER_POINTER:
diff --git a/gcc/testsuite/g++.dg/ext/has-builtin-1.C b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
index 02b4b4d745d..d621171481c 100644
--- a/gcc/testsuite/g++.dg/ext/has-builtin-1.C
+++ b/gcc/testsuite/g++.dg/ext/has-builtin-1.C
@@ -89,6 +89,9 @@
 #if !__has_builtin (__is_function)
 # error "__has_builtin (__is_function) failed"
 #endif
+#if !__has_builtin (__is_integral)
+# error "__has_builtin (__is_integral) failed"
+#endif
 #if !__has_builtin (__is_layout_compatible)
 # error "__has_builtin (__is_layout_compatible) failed"
 #endif
diff --git a/gcc/testsuite/g++.dg/ext/is_integral.C b/gcc/testsuite/g++.dg/ext/is_integral.C
new file mode 100644
index 00000000000..d2c732133dd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/is_integral.C
@@ -0,0 +1,49 @@
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+#define SA_TEST_CATEGORY(TRAIT, TYPE, EXPECT)	\
+  SA(TRAIT(TYPE) == EXPECT);			\
+  SA(TRAIT(const TYPE) == EXPECT);		\
+  SA(TRAIT(volatile TYPE) == EXPECT);		\
+  SA(TRAIT(const volatile TYPE) == EXPECT)
+
+SA_TEST_CATEGORY(__is_integral, void, false);
+
+SA_TEST_CATEGORY(__is_integral, char, true);
+SA_TEST_CATEGORY(__is_integral, signed char, true);
+SA_TEST_CATEGORY(__is_integral, unsigned char, true);
+SA_TEST_CATEGORY(__is_integral, wchar_t, true);
+#ifdef _GLIBCXX_USE_CHAR8_T
+SA_TEST_CATEGORY(__is_integral, char8_t, true);
+#endif
+SA_TEST_CATEGORY(__is_integral, char16_t, true);
+SA_TEST_CATEGORY(__is_integral, char32_t, true);
+SA_TEST_CATEGORY(__is_integral, short, true);
+SA_TEST_CATEGORY(__is_integral, unsigned short, true);
+SA_TEST_CATEGORY(__is_integral, int, true);
+SA_TEST_CATEGORY(__is_integral, unsigned int, true);
+SA_TEST_CATEGORY(__is_integral, long, true);
+SA_TEST_CATEGORY(__is_integral, unsigned long, true);
+SA_TEST_CATEGORY(__is_integral, long long, true);
+SA_TEST_CATEGORY(__is_integral, unsigned long long, true);
+
+SA_TEST_CATEGORY(__is_integral, float, false);
+SA_TEST_CATEGORY(__is_integral, double, false);
+SA_TEST_CATEGORY(__is_integral, long double, false);
+
+#ifndef __STRICT_ANSI__
+// GNU Extensions.
+#ifdef __SIZEOF_INT128__
+SA_TEST_CATEGORY(__is_integral, __int128, true);
+SA_TEST_CATEGORY(__is_integral, unsigned __int128, true);
+#endif
+
+#ifdef _GLIBCXX_USE_FLOAT128
+SA_TEST_CATEGORY(__is_integral, __float128, false);
+#endif
+#endif
+
+// Sanity check.
+class ClassType { };
+SA_TEST_CATEGORY(__is_integral, ClassType, false);
-- 
2.43.0


  reply	other threads:[~2024-01-10 19:41 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10  9:22 [PATCH 00/14] Optimize integral-related type traits Ken Matsui
2024-01-10  9:22 ` Ken Matsui [this message]
2024-01-17  0:30   ` [PATCH 01/14] c++: Implement __is_integral built-in trait Jason Merrill
2024-01-17  7:42     ` Jonathan Wakely
2024-01-17 11:28       ` Joseph Myers
2024-01-10  9:22 ` [PATCH 02/14] libstdc++: Optimize std::is_integral compilation performance Ken Matsui
2024-01-10 21:19   ` Jonathan Wakely
2024-01-10  9:22 ` [PATCH 03/14] c++: Implement __is_floating_point built-in trait Ken Matsui
2024-01-17  0:36   ` Jason Merrill
2024-01-10  9:22 ` [PATCH 04/14] libstdc++: Optimize std::is_floating_point compilation performance Ken Matsui
2024-01-10 21:20   ` Jonathan Wakely
2024-01-10  9:22 ` [PATCH 05/14] c++: Implement __is_arithmetic built-in trait Ken Matsui
2024-01-10  9:22 ` [PATCH 06/14] libstdc++: Optimize std::is_arithmetic compilation performance Ken Matsui
2024-01-10 21:20   ` Jonathan Wakely
2024-01-10  9:22 ` [PATCH 07/14] libstdc++: Optimize std::is_fundamental " Ken Matsui
2024-01-10  9:22 ` [PATCH 08/14] libstdc++: Optimize std::is_compound " Ken Matsui
2024-01-10 21:21   ` Jonathan Wakely
2024-01-11  3:02     ` Ken Matsui
2024-01-11  4:14       ` [committed] " Ken Matsui
2024-01-10  9:23 ` [PATCH 09/14] c++: Implement __is_unsigned built-in trait Ken Matsui
2024-01-17  0:41   ` Jason Merrill
2024-01-10  9:23 ` [PATCH 10/14] libstdc++: Optimize std::is_unsigned compilation performance Ken Matsui
2024-01-10 21:22   ` Jonathan Wakely
2024-01-10  9:23 ` [PATCH 11/14] c++: Implement __is_signed built-in trait Ken Matsui
2024-01-10  9:23 ` [PATCH 12/14] libstdc++: Optimize std::is_signed compilation performance Ken Matsui
2024-01-10 21:22   ` Jonathan Wakely
2024-01-10  9:23 ` [PATCH 13/14] c++: Implement __is_scalar built-in trait Ken Matsui
2024-01-10 19:59   ` Ken Matsui
2024-01-10  9:23 ` [PATCH 14/14] libstdc++: Optimize std::is_scalar compilation performance Ken Matsui
2024-01-10 21:22   ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240110194031.2384005-2-kmatsui@gcc.gnu.org \
    --to=kmatsui@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).