Skip to content

Commit

Permalink
[cfe] Remove FieldBuilder
Browse files Browse the repository at this point in the history
+ 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]>
  • Loading branch information
johnniwinther authored and Commit Queue committed Jan 20, 2025
1 parent 9b4b369 commit e8fdee8
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 127 deletions.
11 changes: 0 additions & 11 deletions pkg/front_end/lib/src/builder/field_builder.dart

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/front_end/lib/src/builder/formal_parameter_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import '../source/builder_factory.dart';
import '../source/constructor_declaration.dart';
import '../source/source_factory_builder.dart';
import '../source/source_library_builder.dart';
import '../source/source_property_builder.dart';
import 'builder.dart';
import 'constructor_builder.dart';
import 'declaration_builders.dart';
import 'member_builder.dart';
import 'omitted_type_builder.dart';
import 'property_builder.dart';
import 'type_builder.dart';
import 'variable_builder.dart';

Expand Down Expand Up @@ -229,7 +229,7 @@ class FormalParameterBuilder extends BuilderImpl
ClassHierarchyBase hierarchy) {
String fieldName = isWildcardLoweredFormalParameter(name) ? '_' : name;
Builder? fieldBuilder = declarationBuilder.lookupLocalMember(fieldName);
if (fieldBuilder is PropertyBuilder && fieldBuilder.isField) {
if (fieldBuilder is SourcePropertyBuilder && fieldBuilder.isField) {
DartType fieldType = fieldBuilder.inferType(hierarchy);
fieldType = constructorDeclaration.substituteFieldType(fieldType);
type.registerInferredType(fieldType);
Expand Down
36 changes: 1 addition & 35 deletions pkg/front_end/lib/src/builder/property_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart';

import 'member_builder.dart';

abstract class PropertyBuilder implements MemberBuilder {
bool get hasInitializer;

@override
Uri get fileUri;

bool get isExtensionTypeDeclaredInstanceField;

bool get isLate;

bool get isFinal;

abstract DartType fieldType;

DartType inferType(ClassHierarchyBase hierarchy);

/// Builds the field initializers for each field used to encode this field
/// using the [fileOffset] for the created nodes and [value] as the initial
/// field value.
List<Initializer> buildInitializer(int fileOffset, Expression value,
{required bool isSynthetic});

/// Creates the AST node for this field as the default initializer.
void buildImplicitDefaultValue();

/// Create the [Initializer] for the implicit initialization of this field
/// in a constructor.
Initializer buildImplicitInitializer();

Initializer buildErroneousInitializer(Expression effect, Expression value,
{required int fileOffset});
}
abstract class PropertyBuilder implements MemberBuilder {}
5 changes: 2 additions & 3 deletions pkg/front_end/lib/src/dill/dill_member_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import 'package:kernel/canonical_name.dart';
import '../builder/builder.dart';
import '../builder/constructor_builder.dart';
import '../builder/declaration_builders.dart';
import '../builder/field_builder.dart';
import '../builder/member_builder.dart';
import '../builder/procedure_builder.dart';
import '../builder/property_builder.dart';
import '../kernel/hierarchy/class_member.dart';
import '../kernel/hierarchy/members_builder.dart' show ClassMembersBuilder;
import '../kernel/member_covariance.dart';
Expand Down Expand Up @@ -122,8 +122,7 @@ abstract class DillMemberBuilder extends MemberBuilderImpl {
Iterable<Annotatable> get annotatables => [member];
}

class DillFieldBuilder extends DillMemberBuilder implements FieldBuilder {
@override
class DillFieldBuilder extends DillMemberBuilder implements PropertyBuilder {
final Field field;

DillFieldBuilder(this.field, super.libraryBuilder,
Expand Down
14 changes: 13 additions & 1 deletion pkg/front_end/lib/src/fragment/field/encoding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,28 @@ sealed class _FieldEncoding {
/// The type of the declared field.
abstract DartType type;

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

/// Creates the AST node for this field as the default initializer.
///
/// This is only used for instance fields.
void buildImplicitDefaultValue();

/// Create the [Initializer] for the implicit initialization of this field
/// Creates the [Initializer] for the implicit initialization of this field
/// in a constructor.
///
/// This is only used for instance fields.
Initializer buildImplicitInitializer();

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/fragment/fragment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import '../kernel/hierarchy/members_builder.dart';
import '../kernel/implicit_field_type.dart';
import '../kernel/internal_ast.dart';
import '../kernel/late_lowering.dart' as late_lowering;
import '../kernel/macro/metadata.dart' hide FieldReference;
import '../kernel/macro/metadata.dart';
import '../kernel/member_covariance.dart';
import '../kernel/type_algorithms.dart';
import '../source/name_scheme.dart';
Expand Down
13 changes: 11 additions & 2 deletions pkg/front_end/lib/src/fragment/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,28 @@ abstract class FieldDeclaration {
/// The [DartType] of this field declaration.
abstract DartType fieldType;

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

/// Creates the AST node for this field as the default initializer.
///
/// This is only used for instance fields.
void buildImplicitDefaultValue();

/// Create the [Initializer] for the implicit initialization of this field
/// Creates the [Initializer] for the implicit initialization of this field
/// in a constructor.
///
/// This is only used for instance fields.
Initializer buildImplicitInitializer();

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

Expand Down
8 changes: 4 additions & 4 deletions pkg/front_end/lib/src/kernel/body_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import '../builder/named_type_builder.dart';
import '../builder/nullability_builder.dart';
import '../builder/omitted_type_builder.dart';
import '../builder/prefix_builder.dart';
import '../builder/property_builder.dart';
import '../builder/record_type_builder.dart';
import '../builder/type_builder.dart';
import '../builder/variable_builder.dart';
Expand All @@ -97,6 +96,7 @@ import '../source/diet_parser.dart';
import '../source/offset_map.dart';
import '../source/source_library_builder.dart';
import '../source/source_member_builder.dart';
import '../source/source_property_builder.dart';
import '../source/stack_listener_impl.dart'
show StackListenerImpl, offsetForToken;
import '../source/value_kinds.dart';
Expand Down Expand Up @@ -9071,7 +9071,7 @@ class BodyBuilder extends StackListenerImpl
}

Initializer buildDuplicatedInitializer(
PropertyBuilder fieldBuilder,
SourcePropertyBuilder fieldBuilder,
Expression value,
String name,
int offset,
Expand Down Expand Up @@ -9107,7 +9107,7 @@ class BodyBuilder extends StackListenerImpl
// Duplicated name, already reported.
while (builder != null) {
if (builder.next == null &&
builder is PropertyBuilder &&
builder is SourcePropertyBuilder &&
builder.isField) {
// Assume the first field has been initialized.
_context.registerInitializedField(builder);
Expand All @@ -9125,7 +9125,7 @@ class BodyBuilder extends StackListenerImpl
),
fieldNameOffset)
];
} else if (builder is PropertyBuilder &&
} else if (builder is SourcePropertyBuilder &&
builder.isField &&
builder.isDeclarationInstanceMember) {
if (builder.isExtensionTypeDeclaredInstanceField) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/front_end/lib/src/kernel/body_builder_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import '../builder/declaration_builders.dart';
import '../builder/formal_parameter_builder.dart';
import '../builder/library_builder.dart';
import '../builder/named_type_builder.dart';
import '../builder/property_builder.dart';
import '../builder/type_builder.dart';
import '../dill/dill_class_builder.dart';
import '../source/constructor_declaration.dart';
Expand All @@ -29,6 +28,7 @@ import '../source/source_factory_builder.dart';
import '../source/source_function_builder.dart';
import '../source/source_library_builder.dart';
import '../source/source_member_builder.dart';
import '../source/source_property_builder.dart';
import '../source/source_type_alias_builder.dart';
import '../type_inference/inference_results.dart'
show InitializerInferenceResult;
Expand Down Expand Up @@ -286,7 +286,7 @@ abstract class BodyBuilderContext {

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

Expand Down Expand Up @@ -816,7 +816,7 @@ mixin _ConstructorBodyBuilderContextMixin<T extends ConstructorDeclaration>
}

@override
void registerInitializedField(PropertyBuilder builder) {
void registerInitializedField(SourcePropertyBuilder builder) {
_member.registerInitializedField(builder);
}

Expand Down
39 changes: 20 additions & 19 deletions pkg/front_end/lib/src/kernel/kernel_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import '../builder/name_iterator.dart';
import '../builder/named_type_builder.dart';
import '../builder/nullability_builder.dart';
import '../builder/procedure_builder.dart';
import '../builder/property_builder.dart';
import '../builder/type_builder.dart';
import '../dill/dill_target.dart' show DillTarget;
import '../source/class_declaration.dart';
Expand All @@ -66,6 +65,7 @@ import '../source/source_library_builder.dart' show SourceLibraryBuilder;
import '../source/source_loader.dart'
show CompilationPhaseForProblemReporting, SourceLoader;
import '../source/source_method_builder.dart';
import '../source/source_property_builder.dart';
import '../type_inference/type_schema.dart';
import 'benchmarker.dart' show BenchmarkPhases, Benchmarker;
import 'cfe_verifier.dart' show verifyComponent, verifyGetStaticType;
Expand Down Expand Up @@ -1532,14 +1532,14 @@ class KernelTarget {

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

Iterator<PropertyBuilder> fieldIterator =
classDeclaration.fullMemberIterator<PropertyBuilder>();
Iterator<SourcePropertyBuilder> fieldIterator =
classDeclaration.fullMemberIterator<SourcePropertyBuilder>();
while (fieldIterator.moveNext()) {
PropertyBuilder fieldBuilder = fieldIterator.current;
SourcePropertyBuilder fieldBuilder = fieldIterator.current;
if (!fieldBuilder.isField) {
continue;
}
Expand All @@ -1561,10 +1561,10 @@ class KernelTarget {
}
}

Map<ConstructorDeclaration, Set<PropertyBuilder>>
Map<ConstructorDeclaration, Set<SourcePropertyBuilder>>
constructorInitializedFields = new Map.identity();
Set<PropertyBuilder>? initializedFieldBuilders = null;
Set<PropertyBuilder>? uninitializedInstanceFields;
Set<SourcePropertyBuilder>? initializedFieldBuilders = null;
Set<SourcePropertyBuilder>? uninitializedInstanceFields;

Iterator<ConstructorDeclaration> constructorIterator =
classDeclaration.fullConstructorIterator<ConstructorDeclaration>();
Expand All @@ -1581,7 +1581,7 @@ class KernelTarget {
nonFinalFields.clear();
}
if (constructor.isConst && lateFinalFields.isNotEmpty) {
for (PropertyBuilder field in lateFinalFields) {
for (SourcePropertyBuilder field in lateFinalFields) {
classDeclaration.addProblem(
messageConstConstructorLateFinalFieldError,
field.fileOffset,
Expand All @@ -1597,23 +1597,24 @@ class KernelTarget {
// Assume that an external constructor initializes all uninitialized
// instance fields.
uninitializedInstanceFields ??= uninitializedFields
.where((PropertyBuilder fieldBuilder) => !fieldBuilder.isStatic)
.where(
(SourcePropertyBuilder fieldBuilder) => !fieldBuilder.isStatic)
.toSet();
constructorInitializedFields[constructor] = uninitializedInstanceFields;
(initializedFieldBuilders ??= new Set<PropertyBuilder>.identity())
(initializedFieldBuilders ??= new Set<SourcePropertyBuilder>.identity())
.addAll(uninitializedInstanceFields);
} else {
Set<PropertyBuilder> fields =
Set<SourcePropertyBuilder> fields =
constructor.takeInitializedFields() ?? const {};
constructorInitializedFields[constructor] = fields;
(initializedFieldBuilders ??= new Set<PropertyBuilder>.identity())
(initializedFieldBuilders ??= new Set<SourcePropertyBuilder>.identity())
.addAll(fields);
}
}

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

// Run through all fields that are initialized by some constructor, and
// make sure that all other constructors also initialize them.
for (MapEntry<ConstructorDeclaration, Set<PropertyBuilder>> entry
for (MapEntry<ConstructorDeclaration, Set<SourcePropertyBuilder>> entry
in constructorInitializedFields.entries) {
ConstructorDeclaration constructorBuilder = entry.key;
Set<PropertyBuilder> fieldBuilders = entry.value;
for (PropertyBuilder fieldBuilder
Set<SourcePropertyBuilder> fieldBuilders = entry.value;
for (SourcePropertyBuilder fieldBuilder
in initializedFieldBuilders!.difference(fieldBuilders)) {
if (fieldBuilder.isExtensionTypeDeclaredInstanceField) continue;
if (!fieldBuilder.hasInitializer && !fieldBuilder.isLate) {
Expand Down
15 changes: 1 addition & 14 deletions pkg/front_end/lib/src/kernel/macro/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import '../../base/scope.dart';
import '../../builder/builder.dart';
import '../../builder/declaration_builders.dart';
import '../../builder/dynamic_type_declaration_builder.dart';
import '../../builder/field_builder.dart';
import '../../builder/future_or_type_declaration_builder.dart';
import '../../builder/member_builder.dart';
import '../../builder/never_type_declaration_builder.dart';
Expand Down Expand Up @@ -89,9 +88,7 @@ shared.Expression? getFieldInitializer(shared.FieldReference reference) {

// Coverage-ignore(suite): Not run.
shared.Proto builderToProto(Builder builder, String name) {
if (builder is FieldBuilder) {
return new shared.FieldProto(new FieldReference(builder));
} else if (builder is PropertyBuilder) {
if (builder is PropertyBuilder) {
return new shared.FieldProto(new PropertyReference(builder));
} else if (builder is ProcedureBuilder) {
return new shared.FunctionProto(new FunctionReference(builder));
Expand Down Expand Up @@ -345,16 +342,6 @@ class PrefixScope implements shared.Scope {
}
}

// Coverage-ignore(suite): Not run.
class FieldReference extends shared.FieldReference {
final FieldBuilder builder;

FieldReference(this.builder);

@override
String get name => builder.name;
}

// Coverage-ignore(suite): Not run.
class PropertyReference extends shared.FieldReference {
final PropertyBuilder builder;
Expand Down
Loading

0 comments on commit e8fdee8

Please sign in to comment.