From aff9a3332df3ab74ae9ee491f76dbd7f61bc73df Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Sun, 8 Nov 2020 02:21:24 -0500 Subject: [PATCH] Bug 568616 - Support for __is_same and __is_same_as (built-in equivalent to std::is_same) IType.isSameType was doing pretty much already what was needed. Added GCC 6.0 and 10.0 parser configs to enable these built-ins for the proper versions. Change-Id: Ifd2908e726c098fb07c9420b29e2cb26014419bf Signed-off-by: Marc-Andre Laperle --- .../core/parser/tests/ast2/AST2CPPTests.java | 70 +++++++++++++++++++ .../core/testplugin/util/BaseTestCase.java | 2 +- .../dom/ast/IASTBinaryTypeIdExpression.java | 6 +- .../cpp/GPPScannerExtensionConfiguration.java | 20 +++++- .../eclipse/cdt/core/parser/GCCKeywords.java | 5 ++ .../eclipse/cdt/core/parser/IGCCToken.java | 5 ++ .../core/dom/parser/ValueFactory.java | 5 ++ .../dom/parser/cpp/GNUCPPSourceParser.java | 4 ++ .../cpp/semantics/EvalBinaryTypeId.java | 1 + 9 files changed, 114 insertions(+), 4 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index 96b72dcd5bf..fe3fce65a9a 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -13574,4 +13574,74 @@ public class AST2CPPTests extends AST2CPPTestBase { public void testExplicitSpecPointerType_562697() throws Exception { parseAndCheckBindings(); } + + // using MyBool = bool; + // + // class Foo { + // }; + // + // template + // class Templated { + // }; + // + // template + // class Test { + // public: + // constexpr static int false_val = 0; + // }; + // + // template<> + // class Test { + // public: + // constexpr static int true_val = 0; + // }; + // + // enum Enum { + // }; + // + // enum EnumChar : char { + // }; + // + // template + // class TemplateArgs { + // public: + // constexpr static bool Value = __is_same(T, U); + // }; + // + // int main() { + // Test<__is_same(bool, bool)>::true_val; + // Test<__is_same_as(bool, bool)>::true_val; + // Test<__is_same(bool, bool&)>::false_val; + // Test<__is_same(bool, bool*)>::false_val; + // Test<__is_same(bool*, bool*)>::true_val; + // Test<__is_same(bool&, bool&)>::true_val; + // Test<__is_same(bool[], bool[])>::true_val; + // Test<__is_same(bool[], bool*)>::false_val; + // Test<__is_same(bool, const volatile MyBool)>::false_val; + // Test<__is_same(bool, MyBool)>::true_val; + // Test<__is_same(bool, Foo)>::false_val; + // Test<__is_same(Templated, Templated)>::false_val; + // Test<__is_same(Templated<>, Templated)>::true_val; + // + // auto func = []() { + // }; + // auto func2 = []() { + // }; + // Test<__is_same(decltype(func), decltype(func))>::true_val; + // Test<__is_same(decltype(func), decltype(func2))>::false_val; + // + // Test<__is_same(void (*)(int), void (*)(int))>::true_val; + // Test<__is_same(void (*)(bool), void (*)(MyBool))>::true_val; + // + // Test<__is_same(Enum, Enum)>::true_val; + // Test<__is_same(Enum, int)>::false_val; + // Test<__is_same(EnumChar, char)>::false_val; + // + // Test::Value>::false_val; + // Test::Value>::true_val; + // } + public void testIsSame() throws Exception { + parseAndCheckBindings(getAboveComment(), CPP, true); + } + } diff --git a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java index 749a8e49ab6..172d940df91 100644 --- a/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java +++ b/core/org.eclipse.cdt.core.tests/suite/org/eclipse/cdt/core/testplugin/util/BaseTestCase.java @@ -73,7 +73,7 @@ public abstract class BaseTestCase extends TestCase { * The GCC version to emulate when running tests. * We emulate the latest version whose extensions we support. */ - protected static final int GCC_MAJOR_VERSION_FOR_TESTS = 8; + protected static final int GCC_MAJOR_VERSION_FOR_TESTS = 10; protected static final int GCC_MINOR_VERSION_FOR_TESTS = 1; /** diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryTypeIdExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryTypeIdExpression.java index abcd5de01a0..3a98edeb2ee 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryTypeIdExpression.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/IASTBinaryTypeIdExpression.java @@ -30,7 +30,11 @@ public interface IASTBinaryTypeIdExpression extends IASTExpression { public static enum Operator { __is_base_of, /** @since 6.0 */ - __is_trivially_assignable + __is_trivially_assignable, + /** + * @since 7.1 + */ + __is_same, } /** diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java index aa659a7e711..99dcd0a0adf 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/cpp/GPPScannerExtensionConfiguration.java @@ -40,14 +40,18 @@ public class GPPScannerExtensionConfiguration extends GNUScannerExtensionConfigu private static final int VERSION_4_6 = version(4, 6); private static final int VERSION_4_7 = version(4, 7); private static final int VERSION_5_0 = version(5, 0); + private static final int VERSION_6_0 = version(6, 0); private static final int VERSION_8_0 = version(8, 0); + private static final int VERSION_10_0 = version(10, 0); private static GPPScannerExtensionConfiguration CONFIG = new GPPScannerExtensionConfiguration(); private static GPPScannerExtensionConfiguration CONFIG_4_2 = new GPPScannerExtensionConfiguration(VERSION_4_2); private static GPPScannerExtensionConfiguration CONFIG_4_3 = new GPPScannerExtensionConfiguration(VERSION_4_3); private static GPPScannerExtensionConfiguration CONFIG_4_6 = new GPPScannerExtensionConfiguration(VERSION_4_6); private static GPPScannerExtensionConfiguration CONFIG_4_7 = new GPPScannerExtensionConfiguration(VERSION_4_7); private static GPPScannerExtensionConfiguration CONFIG_5_0 = new GPPScannerExtensionConfiguration(VERSION_5_0); + private static GPPScannerExtensionConfiguration CONFIG_6_0 = new GPPScannerExtensionConfiguration(VERSION_6_0); private static GPPScannerExtensionConfiguration CONFIG_8_0 = new GPPScannerExtensionConfiguration(VERSION_8_0); + private static GPPScannerExtensionConfiguration CONFIG_10_0 = new GPPScannerExtensionConfiguration(VERSION_10_0); private static GPPScannerExtensionConfiguration CONFIG_CLANG = new GPPScannerExtensionConfiguration( CompilerType.Clang, 0 /* version is ignored for now */); private static GPPScannerExtensionConfiguration CONFIG_MSVC = new GPPScannerExtensionConfiguration( @@ -80,9 +84,15 @@ public class GPPScannerExtensionConfiguration extends GNUScannerExtensionConfigu int major = Integer.valueOf(definedSymbols.get("__GNUC__")); //$NON-NLS-1$ int minor = Integer.valueOf(definedSymbols.get("__GNUC_MINOR__")); //$NON-NLS-1$ int version = version(major, minor); + if (version >= VERSION_10_0) { + return CONFIG_10_0; + } if (version >= VERSION_8_0) { return CONFIG_8_0; } + if (version >= VERSION_6_0) { + return CONFIG_6_0; + } if (version >= VERSION_5_0) { return CONFIG_5_0; } @@ -173,10 +183,16 @@ public class GPPScannerExtensionConfiguration extends GNUScannerExtensionConfigu addKeyword(GCCKeywords.cp__is_trivially_constructible, IGCCToken.tTT_is_trivially_constructible); addKeyword(GCCKeywords.cp__is_trivially_assignable, IGCCToken.tTT_is_trivially_assignable); } + if (version >= VERSION_6_0) { + addKeyword(GCCKeywords.cp__is_same_as, IGCCToken.tTT_is_same); + } if (version >= VERSION_8_0) { addKeyword(GCCKeywords.cp__is_constructible, IGCCToken.tTT_is_constructible); addKeyword(GCCKeywords.cp__integer_pack, IGCCToken.tTT_integer_pack); } + if (version >= VERSION_10_0) { + addKeyword(GCCKeywords.cp__is_same, IGCCToken.tTT_is_same); + } } else if (compiler == CompilerType.Clang) { // As documented at // http://clang.llvm.org/docs/LanguageExtensions.html#checks-for-type-trait-primitives. @@ -230,8 +246,8 @@ public class GPPScannerExtensionConfiguration extends GNUScannerExtensionConfigu addKeyword(GCCKeywords.cp__is_polymorphic, IGCCToken.tTT_is_polymorphic); // __is_reference // __is_rvalue_reference - // __is_same - // __is_same_as + addKeyword(GCCKeywords.cp__is_same, IGCCToken.tTT_is_same); + addKeyword(GCCKeywords.cp__is_same_as, IGCCToken.tTT_is_same); // __is_scalar // __is_sealed // __is_signed diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java index 2062b2ec639..b7374946fce 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/GCCKeywords.java @@ -99,4 +99,9 @@ public class GCCKeywords { * @since 7.1 */ public static final char[] cp__is_literal = "__is_literal".toCharArray(); + + /** + * @since 7.1 + */ + public static final char[] cp__is_same = "__is_same".toCharArray(), cp__is_same_as = "__is_same_as".toCharArray(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/IGCCToken.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/IGCCToken.java index 62033d7c35c..61dd350c9e6 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/IGCCToken.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/IGCCToken.java @@ -96,4 +96,9 @@ public interface IGCCToken extends IToken { /** @since 6.11*/ int tTT_integer_pack = FIRST_RESERVED_IGCCToken + 36; + + /** + * @since 7.1 + */ + int tTT_is_same = FIRST_RESERVED_IGCCToken + 37; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java index 9d104b03465..68651b83224 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ValueFactory.java @@ -603,6 +603,11 @@ public class ValueFactory { return IntegralValue.create(1); } return IntegralValue.create(0); + case __is_same: + if (type1.isSameType(type2)) { + return IntegralValue.create(1); + } + return IntegralValue.create(0); case __is_trivially_assignable: return IntegralValue.UNKNOWN; // TODO: Implement. } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java index 85c3b2de9eb..5ad81884351 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java @@ -1566,6 +1566,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { case IGCCToken.tTT_is_trivially_constructible: case IGCCToken.tTT_is_trivially_assignable: case IGCCToken.tTT_is_constructible: + case IGCCToken.tTT_is_same: return parseTypeTrait(); default: @@ -1617,6 +1618,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { switch (first.getType()) { case IGCCToken.tTT_is_base_of: case IGCCToken.tTT_is_trivially_assignable: + case IGCCToken.tTT_is_same: return true; } return false; @@ -1637,6 +1639,8 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser { return IASTBinaryTypeIdExpression.Operator.__is_base_of; case IGCCToken.tTT_is_trivially_assignable: return IASTBinaryTypeIdExpression.Operator.__is_trivially_assignable; + case IGCCToken.tTT_is_same: + return IASTBinaryTypeIdExpression.Operator.__is_same; } assert false; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalBinaryTypeId.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalBinaryTypeId.java index 21aff0403ca..dc0d2369b94 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalBinaryTypeId.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/EvalBinaryTypeId.java @@ -80,6 +80,7 @@ public class EvalBinaryTypeId extends CPPDependentEvaluation { switch (fOperator) { case __is_base_of: case __is_trivially_assignable: + case __is_same: return CPPBasicType.BOOLEAN; } return ProblemType.UNKNOWN_FOR_EXPRESSION;