Skip to content

Commit ad4d43c

Browse files
jensjohacommit-bot@chromium.org
authored andcommitted
[CFE] Constant transformation for expression compilation
Fixes #42240 Change-Id: Idc75e99aed6acbb67755c77faa1904183a630f91 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153355 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 219935a commit ad4d43c

10 files changed

+120
-24
lines changed

pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart

+26
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,32 @@ void transformLibraries(
127127
}
128128
}
129129

130+
void transformProcedure(
131+
Procedure procedure,
132+
ConstantsBackend backend,
133+
Map<String, String> environmentDefines,
134+
TypeEnvironment typeEnvironment,
135+
ErrorReporter errorReporter,
136+
EvaluationMode evaluationMode,
137+
{bool keepFields: true,
138+
bool evaluateAnnotations: true,
139+
bool desugarSets: false,
140+
bool enableTripleShift: false,
141+
bool errorOnUnevaluatedConstant: false}) {
142+
final ConstantsTransformer constantsTransformer = new ConstantsTransformer(
143+
backend,
144+
environmentDefines,
145+
keepFields,
146+
evaluateAnnotations,
147+
desugarSets,
148+
enableTripleShift,
149+
errorOnUnevaluatedConstant,
150+
typeEnvironment,
151+
errorReporter,
152+
evaluationMode);
153+
constantsTransformer.visitProcedure(procedure);
154+
}
155+
130156
enum EvaluationMode {
131157
weak,
132158
agnostic,

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

+43-21
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ import '../target_implementation.dart' show TargetImplementation;
122122
import '../uri_translator.dart' show UriTranslator;
123123

124124
import 'constant_evaluator.dart' as constants
125-
show EvaluationMode, transformLibraries;
125+
show EvaluationMode, transformLibraries, transformProcedure;
126126

127127
import 'kernel_constants.dart' show KernelConstantErrorReporter;
128128

@@ -1092,26 +1092,7 @@ class KernelTarget extends TargetImplementation {
10921092

10931093
TypeEnvironment environment =
10941094
new TypeEnvironment(loader.coreTypes, loader.hierarchy);
1095-
constants.EvaluationMode evaluationMode;
1096-
// If nnbd is not enabled we will use weak evaluation mode. This is needed
1097-
// because the SDK might be agnostic and therefore needs to be weakened
1098-
// for legacy mode.
1099-
assert(
1100-
isExperimentEnabledGlobally(ExperimentalFlag.nonNullable) ||
1101-
loader.nnbdMode == NnbdMode.Weak,
1102-
"Non-weak nnbd mode found without experiment enabled: "
1103-
"${loader.nnbdMode}.");
1104-
switch (loader.nnbdMode) {
1105-
case NnbdMode.Weak:
1106-
evaluationMode = constants.EvaluationMode.weak;
1107-
break;
1108-
case NnbdMode.Strong:
1109-
evaluationMode = constants.EvaluationMode.strong;
1110-
break;
1111-
case NnbdMode.Agnostic:
1112-
evaluationMode = constants.EvaluationMode.agnostic;
1113-
break;
1114-
}
1095+
constants.EvaluationMode evaluationMode = _getConstantEvaluationMode();
11151096

11161097
constants.transformLibraries(
11171098
loader.libraries,
@@ -1141,11 +1122,52 @@ class KernelTarget extends TargetImplementation {
11411122
ChangedStructureNotifier get changedStructureNotifier => null;
11421123

11431124
void runProcedureTransformations(Procedure procedure) {
1125+
TypeEnvironment environment =
1126+
new TypeEnvironment(loader.coreTypes, loader.hierarchy);
1127+
constants.EvaluationMode evaluationMode = _getConstantEvaluationMode();
1128+
1129+
constants.transformProcedure(
1130+
procedure,
1131+
backendTarget.constantsBackend(loader.coreTypes),
1132+
environmentDefines,
1133+
environment,
1134+
new KernelConstantErrorReporter(loader),
1135+
evaluationMode,
1136+
desugarSets: !backendTarget.supportsSetLiterals,
1137+
enableTripleShift:
1138+
isExperimentEnabledGlobally(ExperimentalFlag.tripleShift),
1139+
errorOnUnevaluatedConstant: errorOnUnevaluatedConstant);
1140+
ticker.logMs("Evaluated constants");
1141+
11441142
backendTarget.performTransformationsOnProcedure(
11451143
loader.coreTypes, loader.hierarchy, procedure,
11461144
logger: (String msg) => ticker.logMs(msg));
11471145
}
11481146

1147+
constants.EvaluationMode _getConstantEvaluationMode() {
1148+
constants.EvaluationMode evaluationMode;
1149+
// If nnbd is not enabled we will use weak evaluation mode. This is needed
1150+
// because the SDK might be agnostic and therefore needs to be weakened
1151+
// for legacy mode.
1152+
assert(
1153+
isExperimentEnabledGlobally(ExperimentalFlag.nonNullable) ||
1154+
loader.nnbdMode == NnbdMode.Weak,
1155+
"Non-weak nnbd mode found without experiment enabled: "
1156+
"${loader.nnbdMode}.");
1157+
switch (loader.nnbdMode) {
1158+
case NnbdMode.Weak:
1159+
evaluationMode = constants.EvaluationMode.weak;
1160+
break;
1161+
case NnbdMode.Strong:
1162+
evaluationMode = constants.EvaluationMode.strong;
1163+
break;
1164+
case NnbdMode.Agnostic:
1165+
evaluationMode = constants.EvaluationMode.agnostic;
1166+
break;
1167+
}
1168+
return evaluationMode;
1169+
}
1170+
11491171
void verify() {
11501172
// TODO(ahe): How to handle errors.
11511173
verifyComponent(component);

pkg/front_end/test/fasta/expression_suite.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ class CompilationResult {
113113
if (compiledProcedure == null) {
114114
buffer.write("<no procedure>");
115115
} else {
116-
new Printer(buffer).visitProcedure(compiledProcedure);
116+
Printer printer = new Printer(buffer);
117+
printer.visitProcedure(compiledProcedure);
118+
printer.writeConstantTable(new Component());
117119
}
118120
Uri base = entryPoint.resolve(".");
119121
return "$buffer".replaceAll("$base", "org-dartlang-testcase:///");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
entry_point: "main.dart"
6+
definitions: []
7+
position: "main.dart"
8+
expression: |
9+
const42.x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Errors: {
2+
}
3+
method /* from org-dartlang-debug:synthetic_debug_expression */ debugExpr() → dynamic
4+
return (#C2).{main::ConstClass::x};
5+
constants {
6+
#C1 = 42
7+
#C2 = main::ConstClass {x:#C1}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
# for details. All rights reserved. Use of this source code is governed by a
3+
# BSD-style license that can be found in the LICENSE file.
4+
5+
entry_point: "main.dart"
6+
definitions: []
7+
position: "main.dart#ConstClass"
8+
expression: |
9+
classConst42.x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Errors: {
2+
}
3+
method /* from org-dartlang-debug:synthetic_debug_expression */ debugExpr() → dynamic
4+
return (#C2).{main::ConstClass::x};
5+
constants {
6+
#C1 = 42
7+
#C2 = main::ConstClass {x:#C1}
8+
}

pkg/front_end/testcases/expression/lib_ctor.expression.yaml.expect

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ Errors: {
33
method /* from org-dartlang-debug:synthetic_debug_expression */ debugExpr() → dynamic
44
return () → dart.core::Null? {
55
new main::A::•<dynamic>();
6-
const main::A::•<dynamic>();
6+
#C2;
77
};
8+
constants {
9+
#C1 = 0
10+
#C2 = main::A<dynamic> {_priv:#C1}
11+
}

pkg/front_end/testcases/expression/main.dart

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ int _privateToplevel(int x) => x + 1;
1414
int globalVar = 6;
1515
int _globalPrivate = 7;
1616

17+
const ConstClass const42 = ConstClass(42);
18+
19+
class ConstClass {
20+
static const ConstClass classConst42 = ConstClass(42);
21+
final int x;
22+
const ConstClass(this.x);
23+
}
24+
1725
class A<T> {
1826
const A();
1927
static int doit(int x) => x + 1;

pkg/frontend_server/test/src/expression_compiler_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ int main() {
11641164
expression: 'const MyClass(1)',
11651165
expectedResult: '''
11661166
(function(p) {
1167-
return dart.const(new foo.MyClass.new(1));
1167+
return C0 || CT.C0;
11681168
}(
11691169
1
11701170
))

0 commit comments

Comments
 (0)