Skip to content

Commit 54c4829

Browse files
joshualittCommit Bot
authored andcommitted
[dart2js] Eliminate a use of a tree node in impact data(1).
This CL moves the error processing for the ConstConstructorInvocation logic to when we build impact data, and in doing so allows us to eliminate one use of a tree node. This should not change any logic. Change-Id: I69ba69cabedb4d73ec718f552d451647c7f0c51b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/239843 Reviewed-by: Sigmund Cherem <sigmund@google.com> Commit-Queue: Joshua Litt <joshualitt@google.com>
1 parent fb9c139 commit 54c4829

File tree

6 files changed

+66
-57
lines changed

6 files changed

+66
-57
lines changed

pkg/compiler/lib/src/ir/impact.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,12 @@ abstract class ImpactRegistry {
182182
void registerRuntimeTypeUse(ir.Expression node, RuntimeTypeUseKind kind,
183183
ir.DartType receiverType, ir.DartType argumentType);
184184

185-
// TODO(johnniwinther): Remove these when CFE provides constants.
186185
void registerConstructorNode(ir.Constructor node);
187186
void registerFieldNode(ir.Field node);
188187
void registerProcedureNode(ir.Procedure node);
189188
void registerStaticInvocationNode(ir.StaticInvocation node);
190189
void registerSwitchStatementNode(ir.SwitchStatement node);
191-
void registerConstConstructorInvocationNode(ir.ConstructorInvocation node);
190+
void registerConstSymbolConstructorInvocationNode();
192191
}
193192

194193
class ImpactBuilderData {

pkg/compiler/lib/src/ir/impact_data.dart

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import 'package:kernel/class_hierarchy.dart' as ir;
99
import 'package:kernel/type_environment.dart' as ir;
1010

1111
import '../common.dart';
12+
import '../common/elements.dart';
13+
import '../elements/entities.dart';
14+
import '../elements/types.dart';
1215
import '../ir/scope.dart';
16+
import '../kernel/element_map.dart';
1317
import '../serialization/serialization.dart';
1418
import '../util/enumset.dart';
1519
import 'constants.dart';
@@ -21,6 +25,7 @@ import 'util.dart';
2125
/// Visitor that builds an [ImpactData] object for the world impact.
2226
class ImpactBuilder extends StaticTypeVisitor implements ImpactRegistry {
2327
final ImpactData _data = ImpactData();
28+
final KernelToElementMap _elementMap;
2429

2530
@override
2631
final VariableScopeModel variableScopeModel;
@@ -34,12 +39,24 @@ class ImpactBuilder extends StaticTypeVisitor implements ImpactRegistry {
3439
@override
3540
final inferEffectivelyFinalVariableTypes;
3641

37-
ImpactBuilder(this.staticTypeContext, StaticTypeCacheImpl staticTypeCache,
38-
ir.ClassHierarchy classHierarchy, this.variableScopeModel,
39-
{this.useAsserts = false, this.inferEffectivelyFinalVariableTypes = true})
42+
ImpactBuilder(
43+
this._elementMap,
44+
this.staticTypeContext,
45+
StaticTypeCacheImpl staticTypeCache,
46+
ir.ClassHierarchy classHierarchy,
47+
this.variableScopeModel,
48+
{this.useAsserts = false,
49+
this.inferEffectivelyFinalVariableTypes = true})
4050
: super(
4151
staticTypeContext.typeEnvironment, classHierarchy, staticTypeCache);
4252

53+
CommonElements get _commonElements => _elementMap.commonElements;
54+
55+
DiagnosticReporter get _reporter => _elementMap.reporter;
56+
57+
String _typeToString(DartType type) =>
58+
type.toStructuredText(_elementMap.types, _elementMap.options);
59+
4360
/// Return the named arguments names as a list of strings.
4461
List<String> _getNamedArguments(ir.Arguments arguments) =>
4562
arguments.named.map((n) => n.name).toList();
@@ -313,6 +330,23 @@ class ImpactBuilder extends StaticTypeVisitor implements ImpactRegistry {
313330
registerProcedureNode(procedure);
314331
}
315332

333+
void _handleConstConstructorInvocation(ir.ConstructorInvocation node) {
334+
assert(node.isConst);
335+
ConstructorEntity constructor = _elementMap.getConstructor(node.target);
336+
if (_commonElements.isSymbolConstructor(constructor)) {
337+
DartType argumentType = _elementMap.getDartType(
338+
node.arguments.positional.first.getStaticType(staticTypeContext));
339+
// TODO(joshualitt): Does the CFE check this for us?
340+
if (argumentType != _commonElements.stringType) {
341+
// TODO(het): Get the actual span for the Symbol constructor argument
342+
_reporter.reportErrorMessage(CURRENT_ELEMENT_SPANNABLE,
343+
MessageKind.STRING_EXPECTED, {'type': _typeToString(argumentType)});
344+
return;
345+
}
346+
registerConstSymbolConstructorInvocationNode();
347+
}
348+
}
349+
316350
@override
317351
void handleConstructorInvocation(ir.ConstructorInvocation node,
318352
ArgumentTypes argumentTypes, ir.DartType resultType) {
@@ -325,7 +359,7 @@ class ImpactBuilder extends StaticTypeVisitor implements ImpactRegistry {
325359
getDeferredImport(node),
326360
isConst: node.isConst);
327361
if (node.isConst) {
328-
registerConstConstructorInvocationNode(node);
362+
_handleConstConstructorInvocation(node);
329363
}
330364
}
331365

@@ -959,13 +993,15 @@ class ImpactBuilder extends StaticTypeVisitor implements ImpactRegistry {
959993
}
960994

961995
@override
962-
void registerConstConstructorInvocationNode(ir.ConstructorInvocation node) {
963-
_data._constConstructorInvocationNodes ??= [];
964-
_data._constConstructorInvocationNodes.add(node);
996+
void registerConstSymbolConstructorInvocationNode() {
997+
_data._hasConstSymbolConstructorInvocation = true;
965998
}
966999
}
9671000

9681001
/// Data object that contains the world impact data derived purely from kernel.
1002+
/// It is critical that all of the data in this class be invariant to changes in
1003+
/// the AST that occur after modular compilation and before deserializing the
1004+
/// impact data.
9691005
class ImpactData {
9701006
static const String tag = 'ImpactData';
9711007

@@ -1012,7 +1048,7 @@ class ImpactData {
10121048
List<ir.Procedure> _procedureNodes;
10131049
List<ir.SwitchStatement> _switchStatementNodes;
10141050
List<ir.StaticInvocation> _staticInvocationNodes;
1015-
List<ir.ConstructorInvocation> _constConstructorInvocationNodes;
1051+
bool _hasConstSymbolConstructorInvocation = false;
10161052

10171053
ImpactData();
10181054

@@ -1109,8 +1145,7 @@ class ImpactData {
11091145
source.readTreeNodes<ir.SwitchStatement>(emptyAsNull: true);
11101146
_staticInvocationNodes =
11111147
source.readTreeNodes<ir.StaticInvocation>(emptyAsNull: true);
1112-
_constConstructorInvocationNodes =
1113-
source.readTreeNodes<ir.ConstructorInvocation>(emptyAsNull: true);
1148+
_hasConstSymbolConstructorInvocation = source.readBool();
11141149
source.end(tag);
11151150
}
11161151

@@ -1186,13 +1221,12 @@ class ImpactData {
11861221
sink.writeList(_runtimeTypeUses, (_RuntimeTypeUse o) => o.toDataSink(sink),
11871222
allowNull: true);
11881223

1189-
// TODO(johnniwinther): Remove these when CFE provides constants.
11901224
sink.writeMemberNodes(_constructorNodes, allowNull: true);
11911225
sink.writeMemberNodes(_fieldNodes, allowNull: true);
11921226
sink.writeMemberNodes(_procedureNodes, allowNull: true);
11931227
sink.writeTreeNodes(_switchStatementNodes, allowNull: true);
11941228
sink.writeTreeNodes(_staticInvocationNodes, allowNull: true);
1195-
sink.writeTreeNodes(_constConstructorInvocationNodes, allowNull: true);
1229+
sink.writeBool(_hasConstSymbolConstructorInvocation);
11961230

11971231
sink.end(tag);
11981232
}
@@ -1527,10 +1561,8 @@ class ImpactData {
15271561
registry.registerStaticInvocationNode(data);
15281562
}
15291563
}
1530-
if (_constConstructorInvocationNodes != null) {
1531-
for (ir.ConstructorInvocation data in _constConstructorInvocationNodes) {
1532-
registry.registerConstConstructorInvocationNode(data);
1533-
}
1564+
if (_hasConstSymbolConstructorInvocation) {
1565+
registry.registerConstSymbolConstructorInvocationNode();
15341566
}
15351567
}
15361568
}

pkg/compiler/lib/src/ir/modular.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// @dart = 2.10
66

77
import 'package:kernel/ast.dart' as ir;
8-
import 'package:kernel/class_hierarchy.dart' as ir;
98
import 'package:kernel/type_environment.dart' as ir;
109

1110
import 'package:front_end/src/api_unstable/dart2js.dart' as ir
@@ -17,7 +16,7 @@ import '../diagnostics/source_span.dart';
1716
import '../ir/impact_data.dart';
1817
import '../ir/static_type.dart';
1918
import '../js_backend/annotations.dart';
20-
import '../options.dart';
19+
import '../kernel/element_map.dart';
2120
import '../serialization/serialization.dart';
2221
import '../util/enumset.dart';
2322
import 'annotations.dart';
@@ -73,19 +72,20 @@ class ModuleData {
7372
}
7473

7574
/// Compute [ModularMemberData] from the IR.
76-
ModularMemberData computeModularMemberData(ir.Member node,
77-
{CompilerOptions options,
78-
ir.TypeEnvironment typeEnvironment,
79-
ir.ClassHierarchy classHierarchy,
75+
ModularMemberData computeModularMemberData(
76+
KernelToElementMap elementMap,
77+
ir.Member node,
8078
ScopeModel scopeModel,
81-
EnumSet<PragmaAnnotation> annotations}) {
79+
EnumSet<PragmaAnnotation> annotations) {
8280
var staticTypeCache = StaticTypeCacheImpl();
8381
var impactBuilderData = ImpactBuilder(
84-
ir.StaticTypeContext(node, typeEnvironment, cache: staticTypeCache),
82+
elementMap,
83+
ir.StaticTypeContext(node, elementMap.typeEnvironment,
84+
cache: staticTypeCache),
8585
staticTypeCache,
86-
classHierarchy,
86+
elementMap.classHierarchy,
8787
scopeModel.variableScopeModel,
88-
useAsserts: options.enableUserAssertions,
88+
useAsserts: elementMap.options.enableUserAssertions,
8989
inferEffectivelyFinalVariableTypes:
9090
!annotations.contains(PragmaAnnotation.disableFinal))
9191
.computeImpact(node);

pkg/compiler/lib/src/kernel/kernel_impact.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,8 @@ class KernelImpactConverter implements ImpactRegistry {
348348
}
349349

350350
@override
351-
void registerConstConstructorInvocationNode(ir.ConstructorInvocation node) {
352-
assert(node.isConst);
353-
ConstructorEntity constructor = elementMap.getConstructor(node.target);
354-
if (commonElements.isSymbolConstructor(constructor)) {
355-
ConstantValue value = elementMap.getConstantValue(
356-
staticTypeContext, node.arguments.positional.first);
357-
if (!value.isString) {
358-
// TODO(het): Get the actual span for the Symbol constructor argument
359-
reporter.reportErrorMessage(
360-
CURRENT_ELEMENT_SPANNABLE,
361-
MessageKind.STRING_EXPECTED,
362-
{'type': typeToString(value.getType(elementMap.commonElements))});
363-
return;
364-
}
365-
registerBackendImpact(_impacts.constSymbol);
366-
}
351+
void registerConstSymbolConstructorInvocationNode() {
352+
registerBackendImpact(_impacts.constSymbol);
367353
}
368354

369355
@override

pkg/compiler/lib/src/kernel/kernel_strategy.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,8 @@ class KernelModularStrategy extends ModularStrategy {
440440
ScopeModel scopeModel = _compilerTask.measureSubtask(
441441
'closures', () => ScopeModel.from(node, _elementMap.constantEvaluator));
442442
return _compilerTask.measureSubtask('worldImpact', () {
443-
return computeModularMemberData(node,
444-
options: _elementMap.options,
445-
typeEnvironment: _elementMap.typeEnvironment,
446-
classHierarchy: _elementMap.classHierarchy,
447-
scopeModel: scopeModel,
448-
annotations: annotations);
443+
return computeModularMemberData(
444+
_elementMap, node, scopeModel, annotations);
449445
});
450446
}
451447
}

pkg/compiler/lib/src/phase/modular_analysis.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ ModuleData run(Input input) {
6363
final scopeModel = ScopeModel.from(member, elementMap.constantEvaluator);
6464
final annotations = processMemberAnnotations(
6565
options, reporter, member, computePragmaAnnotationDataFromIr(member));
66-
result[member] = computeModularMemberData(member,
67-
options: options,
68-
typeEnvironment: elementMap.typeEnvironment,
69-
classHierarchy: elementMap.classHierarchy,
70-
scopeModel: scopeModel,
71-
annotations: annotations)
72-
.impactBuilderData;
66+
result[member] =
67+
computeModularMemberData(elementMap, member, scopeModel, annotations)
68+
.impactBuilderData;
7369
}
7470

7571
for (final library in input.component.libraries) {

0 commit comments

Comments
 (0)