Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMarco Stornelli2020-03-14 13:40:18 +0000
committerMarco Stornelli2020-03-15 15:15:41 +0000
commit16d73e00818bd186c2e62e2cbacca3b88d124e29 (patch)
treeed9defaf9fc107f06b698f3e7a5513d714e21eb0 /core
parentf150522ad4dad9730d9b9acd1c887698a03b02f2 (diff)
downloadorg.eclipse.cdt-16d73e00818bd186c2e62e2cbacca3b88d124e29.tar.gz
org.eclipse.cdt-16d73e00818bd186c2e62e2cbacca3b88d124e29.tar.xz
org.eclipse.cdt-16d73e00818bd186c2e62e2cbacca3b88d124e29.zip
Bug 561128 - Add constexpr evaluation for lambda expressions
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx14/constexpr/FunctionTests.java7
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClosureType.java16
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java15
3 files changed, 31 insertions, 7 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx14/constexpr/FunctionTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx14/constexpr/FunctionTests.java
index 63fb951fef9..ed01e62a1b2 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx14/constexpr/FunctionTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/cxx14/constexpr/FunctionTests.java
@@ -274,4 +274,11 @@ public class FunctionTests extends TestBase {
// .m(21).m(22).m(23).m(24).m(25).m(26).m(27).m(28).m(29).m(30);
public void testLongCallChain_505606() throws Exception {
}
+
+ // auto f = []() constexpr -> int {return 58;};
+
+ // constexpr int x = f();
+ public void testLambdaExpression_560483() throws Exception {
+ assertEvaluationEquals(58);
+ }
} \ No newline at end of file
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClosureType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClosureType.java
index db6b297c23c..204772e384a 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClosureType.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPClosureType.java
@@ -103,7 +103,7 @@ public class CPPClosureType extends PlatformObject implements ICPPClassType, ICP
// Deleted copy assignment operator: A& operator = (const A &)
IType refType = new CPPReferenceType(this, false);
ICPPFunctionType ft = CPPVisitor.createImplicitFunctionType(refType, ps, false, false);
- ICPPMethod m = new CPPImplicitMethod(scope, OverloadableOperator.ASSIGN.toCharArray(), ft, ps, false);
+ CPPImplicitMethod m = new CPPImplicitMethod(scope, OverloadableOperator.ASSIGN.toCharArray(), ft, ps, false);
result[2] = m;
// Destructor: ~A()
@@ -120,22 +120,28 @@ public class CPPClosureType extends PlatformObject implements ICPPClassType, ICP
ICPPParameter[] params = getParameters();
char[] operatorParensName = OverloadableOperator.PAREN.toCharArray();
+ ICPPASTFunctionDeclarator dtor = fLambdaExpression.getDeclarator();
+ boolean constExpr = false;
+ if (dtor != null) {
+ constExpr = dtor.isConstexpr();
+ }
if (isGeneric()) {
m = new CPPImplicitMethodTemplate(getInventedTemplateParameterList(), scope, operatorParensName, ft, params,
- false) {
+ constExpr) {
@Override
public boolean isImplicit() {
return false;
}
};
} else {
- m = new CPPImplicitMethod(scope, operatorParensName, ft, params, false) {
+ m = new CPPImplicitMethod(scope, operatorParensName, ft, params, constExpr) {
@Override
public boolean isImplicit() {
return false;
}
};
}
+ m.addDefinition(fLambdaExpression.getDeclarator());
result[4] = m;
// Conversion operator
@@ -156,14 +162,14 @@ public class CPPClosureType extends PlatformObject implements ICPPClassType, ICP
templateParamClones[i] = (ICPPTemplateParameter) ((IType) templateParams[i]).clone();
}
m = new CPPImplicitMethodTemplate(templateParamClones, scope, conversionOperatorName, ft, params,
- false) {
+ constExpr) {
@Override
public boolean isImplicit() {
return false;
}
};
} else {
- m = new CPPImplicitMethod(scope, conversionOperatorName, ft, params, false) {
+ m = new CPPImplicitMethod(scope, conversionOperatorName, ft, params, constExpr) {
@Override
public boolean isImplicit() {
return false;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java
index e1ba75cbf15..13baf5f1f81 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java
@@ -41,6 +41,7 @@ import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
@@ -625,8 +626,9 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
@Override
public boolean isConstexpr() {
ICPPASTDeclSpecifier declSpec = getDeclSpecifier();
- if (declSpec == null)
+ if (declSpec == null) {
return false;
+ }
return declSpec.isConstexpr();
}
@@ -776,7 +778,16 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
public static ICPPExecution computeFunctionBodyExecution(IASTNode def) {
ICPPASTFunctionDefinition fnDef = getFunctionDefinition(def);
- if (fnDef != null) {
+ if (fnDef == null) {
+ ICPPASTLambdaExpression lambda = ASTQueries.findAncestorWithType(def, ICPPASTLambdaExpression.class);
+ if (lambda == null)
+ return null;
+ ((ASTNode) lambda).resolvePendingAmbiguities();
+ if (lambda.getBody() instanceof CPPASTCompoundStatement) {
+ CPPASTCompoundStatement body = (CPPASTCompoundStatement) lambda.getBody();
+ return body.getExecution();
+ }
+ } else {
// Make sure ambiguity resolution has been performed on the function body, even
// if it's a class method and we're still processing the class declaration.
((ASTNode) fnDef).resolvePendingAmbiguities();

Back to the top