Skip to content

Commit

Permalink
[cfe] Refactor SourceMemberBuilder.checkTypes
Browse files Browse the repository at this point in the history
This expands the signature of SourceMemberBuilder.checkTypes so that it
can fully handle all member checks.

Change-Id: I5414d7ee9f0b7f3be50beffe38b52965be75ddd5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/398360
Reviewed-by: Chloe Stefantsova <[email protected]>
Commit-Queue: Johnni Winther <[email protected]>
  • Loading branch information
johnniwinther authored and Commit Queue committed Dec 2, 2024
1 parent 1207250 commit 9e2d5e0
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 67 deletions.
4 changes: 2 additions & 2 deletions pkg/front_end/lib/src/base/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ mixin ErroneousMemberBuilderMixin implements SourceMemberBuilder {

@override
// Coverage-ignore(suite): Not run.
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
assert(false, "Unexpected call to $runtimeType.checkVariance.");
}

Expand Down
29 changes: 4 additions & 25 deletions pkg/front_end/lib/src/source/source_builder_mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ import '../builder/builder_mixins.dart';
import '../builder/declaration_builders.dart';
import '../builder/library_builder.dart';
import '../builder/member_builder.dart';
import '../builder/procedure_builder.dart';
import '../builder/type_builder.dart';
import '../kernel/body_builder_context.dart';
import '../kernel/kernel_helper.dart';
import '../kernel/type_algorithms.dart';
import 'source_constructor_builder.dart';
import 'source_field_builder.dart';
import 'source_library_builder.dart';
import 'source_loader.dart';
import 'source_member_builder.dart';
import 'source_procedure_builder.dart';

abstract class SourceDeclarationBuilder implements IDeclarationBuilder {
void buildScopes(LibraryBuilder coreLibrary);
Expand Down Expand Up @@ -155,30 +151,13 @@ mixin SourceDeclarationBuilderMixin

void checkTypesInOutline(TypeEnvironment typeEnvironment) {
forEach((String name, Builder builder) {
if (builder is SourceFieldBuilder) {
// Check fields.
libraryBuilder.checkTypesInField(builder, typeEnvironment);
} else if (builder is SourceProcedureBuilder) {
// Check procedures
libraryBuilder.checkTypesInFunctionBuilder(builder, typeEnvironment);
if (builder.isGetter) {
Builder? setterDeclaration =
nameSpace.lookupLocalMember(builder.name, setter: true);
if (setterDeclaration != null) {
libraryBuilder.checkGetterSetterTypes(builder,
setterDeclaration as ProcedureBuilder, typeEnvironment);
}
}
} else {
// Coverage-ignore-block(suite): Not run.
assert(false, "Unexpected member: $builder.");
}
(builder as SourceMemberBuilder)
.checkTypes(libraryBuilder, nameSpace, typeEnvironment);
});

nameSpace.forEachConstructor((String name, MemberBuilder builder) {
if (builder is SourceConstructorBuilder) {
builder.checkTypes(libraryBuilder, typeEnvironment);
}
(builder as SourceMemberBuilder)
.checkTypes(libraryBuilder, nameSpace, typeEnvironment);
});
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/front_end/lib/src/source/source_class_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1152,14 +1152,14 @@ class SourceClassBuilder extends ClassBuilderImpl
while (memberIterator.moveNext()) {
SourceMemberBuilder builder = memberIterator.current;
builder.checkVariance(this, typeEnvironment);
builder.checkTypes(libraryBuilder, typeEnvironment);
builder.checkTypes(libraryBuilder, nameSpace, typeEnvironment);
}

Iterator<SourceMemberBuilder> constructorIterator =
fullConstructorIterator<SourceMemberBuilder>();
while (constructorIterator.moveNext()) {
SourceMemberBuilder builder = constructorIterator.current;
builder.checkTypes(libraryBuilder, typeEnvironment);
builder.checkTypes(libraryBuilder, nameSpace, typeEnvironment);
}
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/front_end/lib/src/source/source_constructor_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import '../base/messages.dart'
messageSuperInitializerNotLast,
noLength;
import '../base/modifiers.dart';
import '../base/name_space.dart';
import '../base/scope.dart';
import '../builder/builder.dart';
import '../builder/constructor_builder.dart';
Expand Down Expand Up @@ -362,8 +363,8 @@ abstract class AbstractSourceConstructorBuilder
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment) {}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
library.checkTypesInConstructorBuilder(this, formals, typeEnvironment);
}

Expand Down Expand Up @@ -986,13 +987,13 @@ class DeclaredSourceConstructorBuilder
}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
super.checkTypes(library, typeEnvironment);
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
super.checkTypes(library, nameSpace, typeEnvironment);
List<DeclaredSourceConstructorBuilder>? augmentations = _augmentations;
if (augmentations != null) {
for (DeclaredSourceConstructorBuilder augmentation in augmentations) {
augmentation.checkTypes(library, typeEnvironment);
augmentation.checkTypes(library, nameSpace, typeEnvironment);
}
}
}
Expand Down Expand Up @@ -1214,8 +1215,8 @@ class SyntheticSourceConstructorBuilder extends MemberBuilderImpl
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment) {}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {}
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {}
}

class SourceExtensionTypeConstructorBuilder
Expand Down
11 changes: 6 additions & 5 deletions pkg/front_end/lib/src/source/source_factory_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import '../base/messages.dart'
templateRedirectingFactoryIncompatibleTypeArgument,
templateTypeArgumentMismatch;
import '../base/modifiers.dart';
import '../base/name_space.dart';
import '../base/problems.dart' show unexpected, unhandled;
import '../base/scope.dart';
import '../builder/builder.dart';
Expand Down Expand Up @@ -318,13 +319,13 @@ class SourceFactoryBuilder extends SourceFunctionBuilderImpl {
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment) {}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
library.checkTypesInFunctionBuilder(this, typeEnvironment);
List<SourceFactoryBuilder>? augmentations = _augmentations;
if (augmentations != null) {
for (SourceFactoryBuilder augmentation in augmentations) {
augmentation.checkTypes(library, typeEnvironment);
augmentation.checkTypes(library, nameSpace, typeEnvironment);
}
}
}
Expand Down Expand Up @@ -655,8 +656,8 @@ class RedirectingFactoryBuilder extends SourceFactoryBuilder {
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment) {}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
library.checkTypesInRedirectingFactoryBuilder(this, typeEnvironment);
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/front_end/lib/src/source/source_field_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// 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:_fe_analyzer_shared/src/scanner/scanner.dart' show Token;
import 'package:_fe_analyzer_shared/src/metadata/expressions.dart' as shared;
import 'package:_fe_analyzer_shared/src/scanner/scanner.dart' show Token;
import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart';
import 'package:kernel/core_types.dart';
Expand All @@ -13,6 +13,7 @@ import 'package:kernel/type_environment.dart';
import '../api_prototype/lowering_predicates.dart';
import '../base/constant_context.dart' show ConstantContext;
import '../base/modifiers.dart' show Modifiers;
import '../base/name_space.dart';
import '../base/problems.dart' show internalProblem;
import '../base/scope.dart' show LookupScope;
import '../builder/builder.dart';
Expand Down Expand Up @@ -652,8 +653,8 @@ class SourceFieldBuilder extends SourceMemberBuilderImpl
}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
library.checkTypesInField(this, typeEnvironment);
}

Expand Down
18 changes: 2 additions & 16 deletions pkg/front_end/lib/src/source/source_library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2009,22 +2009,8 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {
Iterator<Builder> iterator = localMembersIterator;
while (iterator.moveNext()) {
Builder declaration = iterator.current;
if (declaration is SourceFieldBuilder) {
declaration.checkTypes(this, typeEnvironment);
} else if (declaration is SourceProcedureBuilder) {
List<TypeParameterBuilder>? typeParameters = declaration.typeParameters;
if (typeParameters != null && typeParameters.isNotEmpty) {
checkTypeParameterDependencies(typeParameters);
}
declaration.checkTypes(this, typeEnvironment);
if (declaration.isGetter) {
Builder? setterDeclaration = libraryNameSpace
.lookupLocalMember(declaration.name, setter: true);
if (setterDeclaration != null) {
checkGetterSetterTypes(declaration,
setterDeclaration as ProcedureBuilder, typeEnvironment);
}
}
if (declaration is SourceMemberBuilder) {
declaration.checkTypes(this, libraryNameSpace, typeEnvironment);
} else if (declaration is SourceClassBuilder) {
List<TypeParameterBuilder>? typeParameters = declaration.typeParameters;
if (typeParameters != null && typeParameters.isNotEmpty) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/front_end/lib/src/source/source_member_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:kernel/class_hierarchy.dart';
import 'package:kernel/type_environment.dart';

import '../base/common.dart';
import '../base/name_space.dart';
import '../base/problems.dart' show unsupported;
import '../builder/member_builder.dart';
import '../builder/metadata_builder.dart';
Expand Down Expand Up @@ -49,8 +50,8 @@ abstract class SourceMemberBuilder implements MemberBuilder {
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment);

/// Checks the signature types of this member.
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment);
void checkTypes(SourceLibraryBuilder library, NameSpace nameSpace,
TypeEnvironment typeEnvironment);

/// Returns `true` if this member is declared using the `augment` modifier.
bool get isAugmentation;
Expand Down
25 changes: 21 additions & 4 deletions pkg/front_end/lib/src/source/source_procedure_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:kernel/type_algebra.dart';
import 'package:kernel/type_environment.dart';

import '../base/modifiers.dart';
import '../base/name_space.dart';
import '../builder/builder.dart';
import '../builder/declaration_builders.dart';
import '../builder/formal_parameter_builder.dart';
Expand Down Expand Up @@ -672,13 +673,29 @@ class SourceProcedureBuilder extends SourceFunctionBuilderImpl
}

@override
void checkTypes(
SourceLibraryBuilder library, TypeEnvironment typeEnvironment) {
library.checkTypesInFunctionBuilder(this, typeEnvironment);
void checkTypes(SourceLibraryBuilder libraryBuilder, NameSpace nameSpace,
TypeEnvironment typeEnvironment) {
List<TypeParameterBuilder>? typeParameters = this.typeParameters;
if (typeParameters != null && typeParameters.isNotEmpty) {
libraryBuilder.checkTypeParameterDependencies(typeParameters);
}
libraryBuilder.checkTypesInFunctionBuilder(this, typeEnvironment);
List<SourceProcedureBuilder>? augmentations = _augmentations;
if (augmentations != null) {
for (SourceProcedureBuilder augmentation in augmentations) {
augmentation.checkTypes(library, typeEnvironment);
augmentation.checkTypes(libraryBuilder, nameSpace, typeEnvironment);
}
}
if (isGetter) {
if (!isClassMember) {
// Getter/setter type conflict for class members is handled in the class
// hierarchy builder.
Builder? setterDeclaration =
nameSpace.lookupLocalMember(name, setter: true);
if (setterDeclaration != null) {
libraryBuilder.checkGetterSetterTypes(
this, setterDeclaration as ProcedureBuilder, typeEnvironment);
}
}
}
}
Expand Down

0 comments on commit 9e2d5e0

Please sign in to comment.