1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 20:35:38 +02:00

Bug 561128 - Add constexpr evaluation for lambda expressions

Change-Id: I28916218c7d2326407b28170ac051c18ec501f49
This commit is contained in:
Marco Stornelli 2020-03-14 14:40:18 +01:00
parent f150522ad4
commit 16d73e0081
3 changed files with 31 additions and 7 deletions

View file

@ -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);
}
}

View file

@ -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;

View file

@ -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();