From d320f4254282eb7699ae87f2c5af5d5deebe16ce Mon Sep 17 00:00:00 2001 From: Aske Simon Christensen Date: Tue, 2 Apr 2019 12:11:58 +0000 Subject: [PATCH] [CFE] Remove redundant null checks in constant evaluator. The evaluate method now never returns null, so checks for null of return values are redundant, and the "try" in the name of the evaluation functions is not appropriate. Change-Id: I32960164ba73fb706eef5a5a617df7d47e24a074 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98357 Reviewed-by: Johnni Winther Commit-Queue: Aske Simon Christensen --- .../src/fasta/kernel/constant_evaluator.dart | 69 ++++++++----------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart index 85c5de0a830b..bff6654831c2 100644 --- a/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart +++ b/pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart @@ -245,7 +245,7 @@ class ConstantsTransformer extends Transformer { void transformExpressions(List nodes, TreeNode parent) { constantEvaluator.withNewEnvironment(() { for (int i = 0; i < nodes.length; ++i) { - nodes[i] = tryEvaluateAndTransformWithContext(parent, nodes[i]) + nodes[i] = evaluateAndTransformWithContext(parent, nodes[i]) ..parent = parent; } }); @@ -260,7 +260,7 @@ class ConstantsTransformer extends Transformer { transformAnnotations(variable.annotations, variable); if (variable.initializer != null) { variable.initializer = - tryEvaluateAndTransformWithContext(variable, variable.initializer) + evaluateAndTransformWithContext(variable, variable.initializer) ..parent = variable; } } @@ -268,7 +268,7 @@ class ConstantsTransformer extends Transformer { transformAnnotations(variable.annotations, variable); if (variable.initializer != null) { variable.initializer = - tryEvaluateAndTransformWithContext(variable, variable.initializer) + evaluateAndTransformWithContext(variable, variable.initializer) ..parent = variable; } } @@ -283,26 +283,20 @@ class ConstantsTransformer extends Transformer { if (node.initializer != null) { if (node.isConst) { - final Constant constant = - tryEvaluateWithContext(node, node.initializer); - - // If there was a constant evaluation error we will not continue and - // simply keep the old [node]. - if (constant != null) { - constantEvaluator.env.addVariableValue(node, constant); - - if (keepVariables) { - // So the value of the variable is still available for debugging - // purposes we convert the constant variable to be a final variable - // initialized to the evaluated constant expression. - node.initializer = makeConstantExpression(constant)..parent = node; - node.isFinal = true; - node.isConst = false; - } else { - // Since we convert all use-sites of constants, the constant - // [VariableDeclaration] is unused and we'll therefore remove it. - return null; - } + final Constant constant = evaluateWithContext(node, node.initializer); + constantEvaluator.env.addVariableValue(node, constant); + + if (keepVariables) { + // So the value of the variable is still available for debugging + // purposes we convert the constant variable to be a final variable + // initialized to the evaluated constant expression. + node.initializer = makeConstantExpression(constant)..parent = node; + node.isFinal = true; + node.isConst = false; + } else { + // Since we convert all use-sites of constants, the constant + // [VariableDeclaration] is unused and we'll therefore remove it. + return null; } } else { node.initializer = node.initializer.accept(this)..parent = node; @@ -325,7 +319,7 @@ class ConstantsTransformer extends Transformer { transformAnnotations(node.annotations, node); if (node.initializer != null) { node.initializer = - tryEvaluateAndTransformWithContext(node, node.initializer) + evaluateAndTransformWithContext(node, node.initializer) ..parent = node; } } else { @@ -347,11 +341,9 @@ class ConstantsTransformer extends Transformer { visitStaticGet(StaticGet node) { final Member target = node.target; if (target is Field && target.isConst) { - final Constant constant = - tryEvaluateWithContext(node, target.initializer); - return constant != null ? makeConstantExpression(constant) : node; + return evaluateAndTransformWithContext(node, target.initializer); } else if (target is Procedure && target.kind == ProcedureKind.Method) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitStaticGet(node); } @@ -363,42 +355,42 @@ class ConstantsTransformer extends Transformer { visitVariableGet(VariableGet node) { if (node.variable.isConst) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitVariableGet(node); } visitListLiteral(ListLiteral node) { if (node.isConst) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitListLiteral(node); } visitSetLiteral(SetLiteral node) { if (node.isConst) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitSetLiteral(node); } visitMapLiteral(MapLiteral node) { if (node.isConst) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitMapLiteral(node); } visitConstructorInvocation(ConstructorInvocation node) { if (node.isConst) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitConstructorInvocation(node); } visitStaticInvocation(StaticInvocation node) { if (node.isConst) { - return tryEvaluateAndTransformWithContext(node, node); + return evaluateAndTransformWithContext(node, node); } return super.visitStaticInvocation(node); } @@ -407,19 +399,18 @@ class ConstantsTransformer extends Transformer { Constant constant = node.constant; if (constant is UnevaluatedConstant) { Expression expression = constant.expression; - return tryEvaluateAndTransformWithContext(expression, expression); + return evaluateAndTransformWithContext(expression, expression); } else { node.constant = constantEvaluator.canonicalize(constant); return node; } } - tryEvaluateAndTransformWithContext(TreeNode treeContext, Expression node) { - final Constant constant = tryEvaluateWithContext(treeContext, node); - return constant != null ? makeConstantExpression(constant) : node; + evaluateAndTransformWithContext(TreeNode treeContext, Expression node) { + return makeConstantExpression(evaluateWithContext(treeContext, node)); } - tryEvaluateWithContext(TreeNode treeContext, Expression node) { + evaluateWithContext(TreeNode treeContext, Expression node) { if (treeContext == node) { return constantEvaluator.evaluate(node); }