Skip to content

Commit e8fdee8

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Remove FieldBuilder
+ use SourcePropertyBuilder instead of PropertyBuilder in computation of instance field initialization. Change-Id: I832dd082422c70ed02f620f24dcac1fbdd8be0ab Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405021 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 9b4b369 commit e8fdee8

15 files changed

+86
-127
lines changed

pkg/front_end/lib/src/builder/field_builder.dart

Lines changed: 0 additions & 11 deletions
This file was deleted.

pkg/front_end/lib/src/builder/formal_parameter_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import '../source/builder_factory.dart';
2020
import '../source/constructor_declaration.dart';
2121
import '../source/source_factory_builder.dart';
2222
import '../source/source_library_builder.dart';
23+
import '../source/source_property_builder.dart';
2324
import 'builder.dart';
2425
import 'constructor_builder.dart';
2526
import 'declaration_builders.dart';
2627
import 'member_builder.dart';
2728
import 'omitted_type_builder.dart';
28-
import 'property_builder.dart';
2929
import 'type_builder.dart';
3030
import 'variable_builder.dart';
3131

@@ -229,7 +229,7 @@ class FormalParameterBuilder extends BuilderImpl
229229
ClassHierarchyBase hierarchy) {
230230
String fieldName = isWildcardLoweredFormalParameter(name) ? '_' : name;
231231
Builder? fieldBuilder = declarationBuilder.lookupLocalMember(fieldName);
232-
if (fieldBuilder is PropertyBuilder && fieldBuilder.isField) {
232+
if (fieldBuilder is SourcePropertyBuilder && fieldBuilder.isField) {
233233
DartType fieldType = fieldBuilder.inferType(hierarchy);
234234
fieldType = constructorDeclaration.substituteFieldType(fieldType);
235235
type.registerInferredType(fieldType);

pkg/front_end/lib/src/builder/property_builder.dart

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:kernel/ast.dart';
6-
import 'package:kernel/class_hierarchy.dart';
7-
85
import 'member_builder.dart';
96

10-
abstract class PropertyBuilder implements MemberBuilder {
11-
bool get hasInitializer;
12-
13-
@override
14-
Uri get fileUri;
15-
16-
bool get isExtensionTypeDeclaredInstanceField;
17-
18-
bool get isLate;
19-
20-
bool get isFinal;
21-
22-
abstract DartType fieldType;
23-
24-
DartType inferType(ClassHierarchyBase hierarchy);
25-
26-
/// Builds the field initializers for each field used to encode this field
27-
/// using the [fileOffset] for the created nodes and [value] as the initial
28-
/// field value.
29-
List<Initializer> buildInitializer(int fileOffset, Expression value,
30-
{required bool isSynthetic});
31-
32-
/// Creates the AST node for this field as the default initializer.
33-
void buildImplicitDefaultValue();
34-
35-
/// Create the [Initializer] for the implicit initialization of this field
36-
/// in a constructor.
37-
Initializer buildImplicitInitializer();
38-
39-
Initializer buildErroneousInitializer(Expression effect, Expression value,
40-
{required int fileOffset});
41-
}
7+
abstract class PropertyBuilder implements MemberBuilder {}

pkg/front_end/lib/src/dill/dill_member_builder.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import 'package:kernel/canonical_name.dart';
1818
import '../builder/builder.dart';
1919
import '../builder/constructor_builder.dart';
2020
import '../builder/declaration_builders.dart';
21-
import '../builder/field_builder.dart';
2221
import '../builder/member_builder.dart';
2322
import '../builder/procedure_builder.dart';
23+
import '../builder/property_builder.dart';
2424
import '../kernel/hierarchy/class_member.dart';
2525
import '../kernel/hierarchy/members_builder.dart' show ClassMembersBuilder;
2626
import '../kernel/member_covariance.dart';
@@ -122,8 +122,7 @@ abstract class DillMemberBuilder extends MemberBuilderImpl {
122122
Iterable<Annotatable> get annotatables => [member];
123123
}
124124

125-
class DillFieldBuilder extends DillMemberBuilder implements FieldBuilder {
126-
@override
125+
class DillFieldBuilder extends DillMemberBuilder implements PropertyBuilder {
127126
final Field field;
128127

129128
DillFieldBuilder(this.field, super.libraryBuilder,

pkg/front_end/lib/src/fragment/field/encoding.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,28 @@ sealed class _FieldEncoding {
3232
/// The type of the declared field.
3333
abstract DartType type;
3434

35+
/// Builds the [Initializer]s for each field used to encode this field
36+
/// using the [fileOffset] for the created nodes and [value] as the initial
37+
/// field value.
38+
///
39+
/// This is only used for instance fields.
3540
List<Initializer> createInitializer(int fileOffset, Expression value,
3641
{required bool isSynthetic});
3742

3843
/// Creates the AST node for this field as the default initializer.
44+
///
45+
/// This is only used for instance fields.
3946
void buildImplicitDefaultValue();
4047

41-
/// Create the [Initializer] for the implicit initialization of this field
48+
/// Creates the [Initializer] for the implicit initialization of this field
4249
/// in a constructor.
50+
///
51+
/// This is only used for instance fields.
4352
Initializer buildImplicitInitializer();
4453

54+
/// Creates the [Initializer] for the invalid initialization of this field.
55+
///
56+
/// This is only used for instance fields.
4557
Initializer buildErroneousInitializer(Expression effect, Expression value,
4658
{required int fileOffset});
4759

pkg/front_end/lib/src/fragment/fragment.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import '../kernel/hierarchy/members_builder.dart';
3838
import '../kernel/implicit_field_type.dart';
3939
import '../kernel/internal_ast.dart';
4040
import '../kernel/late_lowering.dart' as late_lowering;
41-
import '../kernel/macro/metadata.dart' hide FieldReference;
41+
import '../kernel/macro/metadata.dart';
4242
import '../kernel/member_covariance.dart';
4343
import '../kernel/type_algorithms.dart';
4444
import '../source/name_scheme.dart';

pkg/front_end/lib/src/fragment/util.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,28 @@ abstract class FieldDeclaration {
171171
/// The [DartType] of this field declaration.
172172
abstract DartType fieldType;
173173

174+
/// Creates the [Initializer] for the invalid initialization of this field.
175+
///
176+
/// This is only used for instance fields.
174177
Initializer buildErroneousInitializer(Expression effect, Expression value,
175178
{required int fileOffset});
176179

177180
/// Creates the AST node for this field as the default initializer.
181+
///
182+
/// This is only used for instance fields.
178183
void buildImplicitDefaultValue();
179184

180-
/// Create the [Initializer] for the implicit initialization of this field
185+
/// Creates the [Initializer] for the implicit initialization of this field
181186
/// in a constructor.
187+
///
188+
/// This is only used for instance fields.
182189
Initializer buildImplicitInitializer();
183190

184-
/// Builds the field initializers for each field used to encode this field
191+
/// Builds the [Initializer]s for each field used to encode this field
185192
/// using the [fileOffset] for the created nodes and [value] as the initial
186193
/// field value.
194+
///
195+
/// This is only used for instance fields.
187196
List<Initializer> buildInitializer(int fileOffset, Expression value,
188197
{required bool isSynthetic});
189198

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ import '../builder/named_type_builder.dart';
7373
import '../builder/nullability_builder.dart';
7474
import '../builder/omitted_type_builder.dart';
7575
import '../builder/prefix_builder.dart';
76-
import '../builder/property_builder.dart';
7776
import '../builder/record_type_builder.dart';
7877
import '../builder/type_builder.dart';
7978
import '../builder/variable_builder.dart';
@@ -97,6 +96,7 @@ import '../source/diet_parser.dart';
9796
import '../source/offset_map.dart';
9897
import '../source/source_library_builder.dart';
9998
import '../source/source_member_builder.dart';
99+
import '../source/source_property_builder.dart';
100100
import '../source/stack_listener_impl.dart'
101101
show StackListenerImpl, offsetForToken;
102102
import '../source/value_kinds.dart';
@@ -9071,7 +9071,7 @@ class BodyBuilder extends StackListenerImpl
90719071
}
90729072

90739073
Initializer buildDuplicatedInitializer(
9074-
PropertyBuilder fieldBuilder,
9074+
SourcePropertyBuilder fieldBuilder,
90759075
Expression value,
90769076
String name,
90779077
int offset,
@@ -9107,7 +9107,7 @@ class BodyBuilder extends StackListenerImpl
91079107
// Duplicated name, already reported.
91089108
while (builder != null) {
91099109
if (builder.next == null &&
9110-
builder is PropertyBuilder &&
9110+
builder is SourcePropertyBuilder &&
91119111
builder.isField) {
91129112
// Assume the first field has been initialized.
91139113
_context.registerInitializedField(builder);
@@ -9125,7 +9125,7 @@ class BodyBuilder extends StackListenerImpl
91259125
),
91269126
fieldNameOffset)
91279127
];
9128-
} else if (builder is PropertyBuilder &&
9128+
} else if (builder is SourcePropertyBuilder &&
91299129
builder.isField &&
91309130
builder.isDeclarationInstanceMember) {
91319131
if (builder.isExtensionTypeDeclaredInstanceField) {

pkg/front_end/lib/src/kernel/body_builder_context.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import '../builder/declaration_builders.dart';
1515
import '../builder/formal_parameter_builder.dart';
1616
import '../builder/library_builder.dart';
1717
import '../builder/named_type_builder.dart';
18-
import '../builder/property_builder.dart';
1918
import '../builder/type_builder.dart';
2019
import '../dill/dill_class_builder.dart';
2120
import '../source/constructor_declaration.dart';
@@ -29,6 +28,7 @@ import '../source/source_factory_builder.dart';
2928
import '../source/source_function_builder.dart';
3029
import '../source/source_library_builder.dart';
3130
import '../source/source_member_builder.dart';
31+
import '../source/source_property_builder.dart';
3232
import '../source/source_type_alias_builder.dart';
3333
import '../type_inference/inference_results.dart'
3434
show InitializerInferenceResult;
@@ -286,7 +286,7 @@ abstract class BodyBuilderContext {
286286

287287
/// Registers that the field [builder] has been initialized in generative
288288
/// constructor whose body is being built.
289-
void registerInitializedField(PropertyBuilder builder) {
289+
void registerInitializedField(SourcePropertyBuilder builder) {
290290
throw new UnsupportedError('${runtimeType}.registerInitializedField');
291291
}
292292

@@ -816,7 +816,7 @@ mixin _ConstructorBodyBuilderContextMixin<T extends ConstructorDeclaration>
816816
}
817817

818818
@override
819-
void registerInitializedField(PropertyBuilder builder) {
819+
void registerInitializedField(SourcePropertyBuilder builder) {
820820
_member.registerInitializedField(builder);
821821
}
822822

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import '../builder/name_iterator.dart';
5353
import '../builder/named_type_builder.dart';
5454
import '../builder/nullability_builder.dart';
5555
import '../builder/procedure_builder.dart';
56-
import '../builder/property_builder.dart';
5756
import '../builder/type_builder.dart';
5857
import '../dill/dill_target.dart' show DillTarget;
5958
import '../source/class_declaration.dart';
@@ -66,6 +65,7 @@ import '../source/source_library_builder.dart' show SourceLibraryBuilder;
6665
import '../source/source_loader.dart'
6766
show CompilationPhaseForProblemReporting, SourceLoader;
6867
import '../source/source_method_builder.dart';
68+
import '../source/source_property_builder.dart';
6969
import '../type_inference/type_schema.dart';
7070
import 'benchmarker.dart' show BenchmarkPhases, Benchmarker;
7171
import 'cfe_verifier.dart' show verifyComponent, verifyGetStaticType;
@@ -1532,14 +1532,14 @@ class KernelTarget {
15321532

15331533
/// Quotes below are from [Dart Programming Language Specification, 4th
15341534
/// Edition](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-408.pdf):
1535-
List<PropertyBuilder> uninitializedFields = [];
1536-
List<PropertyBuilder> nonFinalFields = [];
1537-
List<PropertyBuilder> lateFinalFields = [];
1535+
List<SourcePropertyBuilder> uninitializedFields = [];
1536+
List<SourcePropertyBuilder> nonFinalFields = [];
1537+
List<SourcePropertyBuilder> lateFinalFields = [];
15381538

1539-
Iterator<PropertyBuilder> fieldIterator =
1540-
classDeclaration.fullMemberIterator<PropertyBuilder>();
1539+
Iterator<SourcePropertyBuilder> fieldIterator =
1540+
classDeclaration.fullMemberIterator<SourcePropertyBuilder>();
15411541
while (fieldIterator.moveNext()) {
1542-
PropertyBuilder fieldBuilder = fieldIterator.current;
1542+
SourcePropertyBuilder fieldBuilder = fieldIterator.current;
15431543
if (!fieldBuilder.isField) {
15441544
continue;
15451545
}
@@ -1561,10 +1561,10 @@ class KernelTarget {
15611561
}
15621562
}
15631563

1564-
Map<ConstructorDeclaration, Set<PropertyBuilder>>
1564+
Map<ConstructorDeclaration, Set<SourcePropertyBuilder>>
15651565
constructorInitializedFields = new Map.identity();
1566-
Set<PropertyBuilder>? initializedFieldBuilders = null;
1567-
Set<PropertyBuilder>? uninitializedInstanceFields;
1566+
Set<SourcePropertyBuilder>? initializedFieldBuilders = null;
1567+
Set<SourcePropertyBuilder>? uninitializedInstanceFields;
15681568

15691569
Iterator<ConstructorDeclaration> constructorIterator =
15701570
classDeclaration.fullConstructorIterator<ConstructorDeclaration>();
@@ -1581,7 +1581,7 @@ class KernelTarget {
15811581
nonFinalFields.clear();
15821582
}
15831583
if (constructor.isConst && lateFinalFields.isNotEmpty) {
1584-
for (PropertyBuilder field in lateFinalFields) {
1584+
for (SourcePropertyBuilder field in lateFinalFields) {
15851585
classDeclaration.addProblem(
15861586
messageConstConstructorLateFinalFieldError,
15871587
field.fileOffset,
@@ -1597,23 +1597,24 @@ class KernelTarget {
15971597
// Assume that an external constructor initializes all uninitialized
15981598
// instance fields.
15991599
uninitializedInstanceFields ??= uninitializedFields
1600-
.where((PropertyBuilder fieldBuilder) => !fieldBuilder.isStatic)
1600+
.where(
1601+
(SourcePropertyBuilder fieldBuilder) => !fieldBuilder.isStatic)
16011602
.toSet();
16021603
constructorInitializedFields[constructor] = uninitializedInstanceFields;
1603-
(initializedFieldBuilders ??= new Set<PropertyBuilder>.identity())
1604+
(initializedFieldBuilders ??= new Set<SourcePropertyBuilder>.identity())
16041605
.addAll(uninitializedInstanceFields);
16051606
} else {
1606-
Set<PropertyBuilder> fields =
1607+
Set<SourcePropertyBuilder> fields =
16071608
constructor.takeInitializedFields() ?? const {};
16081609
constructorInitializedFields[constructor] = fields;
1609-
(initializedFieldBuilders ??= new Set<PropertyBuilder>.identity())
1610+
(initializedFieldBuilders ??= new Set<SourcePropertyBuilder>.identity())
16101611
.addAll(fields);
16111612
}
16121613
}
16131614

16141615
// Run through all fields that aren't initialized by any constructor, and
16151616
// set their initializer to `null`.
1616-
for (PropertyBuilder fieldBuilder in uninitializedFields) {
1617+
for (SourcePropertyBuilder fieldBuilder in uninitializedFields) {
16171618
if (fieldBuilder.isExtensionTypeDeclaredInstanceField) continue;
16181619
if (initializedFieldBuilders == null ||
16191620
!initializedFieldBuilders.contains(fieldBuilder)) {
@@ -1653,11 +1654,11 @@ class KernelTarget {
16531654

16541655
// Run through all fields that are initialized by some constructor, and
16551656
// make sure that all other constructors also initialize them.
1656-
for (MapEntry<ConstructorDeclaration, Set<PropertyBuilder>> entry
1657+
for (MapEntry<ConstructorDeclaration, Set<SourcePropertyBuilder>> entry
16571658
in constructorInitializedFields.entries) {
16581659
ConstructorDeclaration constructorBuilder = entry.key;
1659-
Set<PropertyBuilder> fieldBuilders = entry.value;
1660-
for (PropertyBuilder fieldBuilder
1660+
Set<SourcePropertyBuilder> fieldBuilders = entry.value;
1661+
for (SourcePropertyBuilder fieldBuilder
16611662
in initializedFieldBuilders!.difference(fieldBuilders)) {
16621663
if (fieldBuilder.isExtensionTypeDeclaredInstanceField) continue;
16631664
if (!fieldBuilder.hasInitializer && !fieldBuilder.isLate) {

0 commit comments

Comments
 (0)