From 058be76a2cc894d444af575acaa2b36ca1576086 Mon Sep 17 00:00:00 2001 From: "Igor V. Kovalenko" Date: Tue, 14 Mar 2023 19:23:55 +0300 Subject: [PATCH] Add __builtin_is_constant_evaluated --- .../cdt/core/parser/tests/ast2/AST2CPPTests.java | 6 ++++++ .../core/dom/parser/GCCBuiltinSymbolProvider.java | 4 ++++ .../core/dom/parser/cpp/semantics/ExecBuiltin.java | 10 +++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) 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 a0883238103..8c20a9f5b4a 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 @@ -13872,4 +13872,10 @@ public void testArithmeticEvaluationOfRelationalOps() throws Exception { BindingAssertionHelper helper = getAssertionHelper(); helper.assertVariableValue("shiftpack", 3); } + + // constexpr auto true_value = __builtin_is_constant_evaluated(); + public void testBuiltinIsConstantEvaluated() throws Exception { + BindingAssertionHelper helper = getAssertionHelper(); + helper.assertVariableValue("true_value", 1); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/GCCBuiltinSymbolProvider.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/GCCBuiltinSymbolProvider.java index cfe08adf4ba..2211394a855 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/GCCBuiltinSymbolProvider.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/GCCBuiltinSymbolProvider.java @@ -226,6 +226,7 @@ private void addGnuBuiltins() { ICPPExecution builtinClz = new ExecBuiltin(ExecBuiltin.BUILTIN_CLZ); ICPPExecution builtinClzl = new ExecBuiltin(ExecBuiltin.BUILTIN_CLZL); ICPPExecution builtinClzll = new ExecBuiltin(ExecBuiltin.BUILTIN_CLZLL); + ICPPExecution builtinIsConstantEvaluated = new ExecBuiltin(ExecBuiltin.BUILTIN_IS_CONSTANT_EVALUATED); // Other Builtins (https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html) [incomplete] function("void", "__builtin_abort"); @@ -358,6 +359,9 @@ private void addGnuBuiltins() { function("_Decimal128", "__builtin_infd128"); function("float", "__builtin_inff"); function("long double", "__builtin_infl"); + if (fCpp) { + function("bool", "__builtin_is_constant_evaluated", builtinIsConstantEvaluated); + } function("int", "__builtin_isinf_sign", "..."); function("bool", "__builtin_isfinite", "double"); function("bool", "__builtin_isgreater", "float", "float"); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/ExecBuiltin.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/ExecBuiltin.java index 54575701009..c6ad2f21645 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/ExecBuiltin.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/ExecBuiltin.java @@ -37,7 +37,7 @@ public class ExecBuiltin implements ICPPExecution { BUILTIN_CTZLL = 5, BUILTIN_POPCOUNT = 6, BUILTIN_POPCOUNTL = 7, BUILTIN_POPCOUNTLL = 8, BUILTIN_PARITY = 9, BUILTIN_PARITYL = 10, BUILTIN_PARITYLL = 11, BUILTIN_ABS = 12, BUILTIN_LABS = 13, BUILTIN_LLABS = 14, BUILTIN_CLRSB = 15, BUILTIN_CLRSBL = 16, BUILTIN_CLRSBLL = 17, BUILTIN_CLZ = 18, BUILTIN_CLZL = 19, - BUILTIN_CLZLL = 20; + BUILTIN_CLZLL = 20, BUILTIN_IS_CONSTANT_EVALUATED = 21; private static IType intType = new CPPBasicType(Kind.eInt, 0); private static IType longType = new CPPBasicType(Kind.eInt, CPPBasicType.IS_LONG); @@ -100,6 +100,8 @@ public ICPPExecution executeForFunctionCall(ActivationRecord record, ConstexprEv return executeBuiltinClz(record, context, longType); case BUILTIN_CLZLL: return executeBuiltinClz(record, context, longlongType); + case BUILTIN_IS_CONSTANT_EVALUATED: + return executeBuiltinIsConstantEvaluated(record, context, null); } return null; } @@ -264,6 +266,12 @@ private ICPPExecution executeBuiltinClz(ActivationRecord record, ConstexprEvalua return new ExecReturn(new EvalFixed(intType, ValueCategory.PRVALUE, IntegralValue.create(result))); } + private ICPPExecution executeBuiltinIsConstantEvaluated(ActivationRecord record, ConstexprEvaluationContext context, + IType argType) { + // Since this is only evaluated in constexpr evaluation context, return true + return new ExecReturn(new EvalFixed(intType, ValueCategory.PRVALUE, IntegralValue.create(1))); + } + @Override public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException { buffer.putShort(ITypeMarshalBuffer.EXEC_BUILTIN);