Skip to content

Commit

Permalink
[cfe] Create SourceProcedureBuilder through fragments
Browse files Browse the repository at this point in the history
Change-Id: If7fe7cc1b7779950e2d2a0227dde20c02eafddbf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/385440
Commit-Queue: Johnni Winther <[email protected]>
Reviewed-by: Jens Johansen <[email protected]>
  • Loading branch information
johnniwinther authored and Commit Queue committed Sep 17, 2024
1 parent 6d1f171 commit df4e73c
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 51 deletions.
1 change: 1 addition & 0 deletions pkg/front_end/lib/src/builder/member_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ abstract class MemberBuilderImpl extends ModifierBuilderImpl
@override
LibraryBuilder get libraryBuilder {
if (parent is LibraryBuilder) {
// Coverage-ignore-block(suite): Not run.
LibraryBuilder library = parent as LibraryBuilder;
return library.partOfLibrary ?? library;
} else if (parent is ExtensionBuilder) {
Expand Down
61 changes: 61 additions & 0 deletions pkg/front_end/lib/src/fragment/fragment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../base/scope.dart';
import '../builder/builder.dart';
import '../builder/constructor_reference_builder.dart';
import '../builder/declaration_builders.dart';
import '../builder/formal_parameter_builder.dart';
import '../builder/metadata_builder.dart';
import '../builder/mixin_application_builder.dart';
import '../builder/type_builder.dart';
Expand All @@ -19,6 +20,7 @@ import '../source/source_enum_builder.dart';
import '../source/source_extension_builder.dart';
import '../source/source_extension_type_declaration_builder.dart';
import '../source/source_field_builder.dart';
import '../source/source_procedure_builder.dart';
import '../source/source_type_alias_builder.dart';
import '../source/type_parameter_scope_builder.dart';

Expand Down Expand Up @@ -525,3 +527,62 @@ class FieldFragment implements Fragment {
@override
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
}

class MethodFragment implements Fragment {
final String name;
final Uri fileUri;
final int startCharOffset;
final int charOffset;
final int charOpenParenOffset;
final int charEndOffset;
final List<MetadataBuilder>? metadata;
final int modifiers;
final TypeBuilder returnType;
final List<NominalVariableBuilder>? typeParameters;
final List<FormalParameterBuilder>? formals;
final ProcedureKind kind;
final Reference? procedureReference;
final Reference? tearOffReference;
final AsyncMarker asyncModifier;
final NameScheme nameScheme;
final String? nativeMethodName;

SourceProcedureBuilder? _builder;

MethodFragment(
{required this.name,
required this.fileUri,
required this.startCharOffset,
required this.charOffset,
required this.charOpenParenOffset,
required this.charEndOffset,
required this.metadata,
required this.modifiers,
required this.returnType,
required this.typeParameters,
required this.formals,
required this.kind,
required this.procedureReference,
required this.tearOffReference,
required this.asyncModifier,
required this.nameScheme,
required this.nativeMethodName});

@override
SourceProcedureBuilder get builder {
assert(
_builder != null, // Coverage-ignore(suite): Not run.
"Builder has not been computed for $this.");
return _builder!;
}

void set builder(SourceProcedureBuilder value) {
assert(
_builder == null, // Coverage-ignore(suite): Not run.
"Builder has already been computed for $this.");
_builder = value;
}

@override
String toString() => '$runtimeType($name,$fileUri,$charOffset)';
}
10 changes: 5 additions & 5 deletions pkg/front_end/lib/src/source/offset_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OffsetMap {
final Map<int, DeclarationFragment> _declarationFragments = {};
final Map<int, FieldFragment> _fields = {};
final Map<int, SourceFunctionBuilder> _constructors = {};
final Map<int, SourceFunctionBuilder> _procedures = {};
final Map<int, MethodFragment> _procedures = {};
final Map<int, LibraryPart> _parts = {};
final Map<int, Import> _imports = {};
final Map<int, Export> _exports = {};
Expand Down Expand Up @@ -121,13 +121,13 @@ class OffsetMap {
identifier.nameOffset);
}

void registerProcedure(Identifier identifier, SourceFunctionBuilder builder) {
_procedures[identifier.nameOffset] = builder;
void registerProcedure(Identifier identifier, MethodFragment fragment) {
_procedures[identifier.nameOffset] = fragment;
}

SourceFunctionBuilder lookupProcedure(Identifier identifier) {
return _checkBuilder(_procedures[identifier.nameOffset], identifier.name,
identifier.nameOffset);
return _checkBuilder(_procedures[identifier.nameOffset]?.builder,
identifier.name, identifier.nameOffset);
}

T _checkDirective<T>(T? directive, String name, int charOffset) {
Expand Down
55 changes: 31 additions & 24 deletions pkg/front_end/lib/src/source/source_builder_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import 'source_factory_builder.dart';
import 'source_function_builder.dart';
import 'source_library_builder.dart';
import 'source_loader.dart' show SourceLoader;
import 'source_procedure_builder.dart';
import 'type_parameter_scope_builder.dart';

class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
Expand Down Expand Up @@ -125,6 +124,8 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {

final List<SourceFunctionBuilder> _nativeMethods = [];

final List<MethodFragment> _nativeMethodFragments = [];

final LibraryName libraryName;

final LookupScope _compilationUnitScope;
Expand Down Expand Up @@ -2124,6 +2125,10 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
return suffix;
}

void _addNativeMethodFragment(MethodFragment fragment) {
_nativeMethodFragments.add(fragment);
}

void _addNativeMethod(SourceFunctionBuilder method) {
_nativeMethods.add(method);
}
Expand Down Expand Up @@ -2203,34 +2208,32 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
}
}
}
SourceProcedureBuilder procedureBuilder = new SourceProcedureBuilder(
metadata,
modifiers,
returnType ?? addInferableType(),
name,
typeVariables,
formals,
kind,
_parent,
_compilationUnit.fileUri,
startCharOffset,
charOffset,
charOpenParenOffset,
charEndOffset,
procedureReference,
tearOffReference,
asyncModifier,
nameScheme,
MethodFragment fragment = new MethodFragment(
name: name,
fileUri: _compilationUnit.fileUri,
startCharOffset: startCharOffset,
charOffset: charOffset,
charOpenParenOffset: charOpenParenOffset,
charEndOffset: charEndOffset,
metadata: metadata,
modifiers: modifiers,
returnType: returnType ?? addInferableType(),
typeParameters: typeVariables,
formals: formals,
kind: kind,
procedureReference: procedureReference,
tearOffReference: tearOffReference,
asyncModifier: asyncModifier,
nameScheme: nameScheme,
nativeMethodName: nativeMethodName);
_nominalParameterNameSpaces.pop().addTypeVariables(
_problemReporting, typeVariables,
ownerName: procedureBuilder.name, allowNameConflict: true);
_addBuilder(name, procedureBuilder, charOffset,
getterReference: procedureReference);
ownerName: name, allowNameConflict: true);
_addFragment(fragment, getterReference: procedureReference);
if (nativeMethodName != null) {
_addNativeMethod(procedureBuilder);
_addNativeMethodFragment(fragment);
}
offsetMap.registerProcedure(identifier, procedureBuilder);
offsetMap.registerProcedure(identifier, fragment);
}

@override
Expand Down Expand Up @@ -2662,6 +2665,7 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
loader.buildersCreatedWithReferences[setterReference] = declaration;
}
if (_declarationFragments.isEmpty) {
// Coverage-ignore-block(suite): Not run.
_libraryNameSpaceBuilder.addBuilder(
name, declaration, _compilationUnit.fileUri, charOffset);
} else {
Expand Down Expand Up @@ -2742,6 +2746,9 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
for (SourceFunctionBuilder method in _nativeMethods) {
method.becomeNative(loader);
}
for (MethodFragment fragment in _nativeMethodFragments) {
fragment.builder.becomeNative(loader);
}
return _nativeMethods.length;
}

Expand Down
1 change: 1 addition & 0 deletions pkg/front_end/lib/src/source/source_enum_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ class SourceEnumBuilder extends SourceClassBuilder {
/* formals = */ null,
ProcedureKind.Method,
libraryBuilder,
this,
fileUri,
charOffset,
charOffset,
Expand Down
27 changes: 13 additions & 14 deletions pkg/front_end/lib/src/source/source_function_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@ import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart';

import '../api_prototype/lowering_predicates.dart';
import '../base/identifiers.dart';
import '../base/local_scope.dart';
import '../base/messages.dart'
show
messagePatchDeclarationMismatch,
messagePatchDeclarationOrigin,
messagePatchNonExternal,
noLength,
templateRequiredNamedParameterHasDefaultValueError;
import '../base/modifier.dart';
import '../base/scope.dart';
import '../builder/builder.dart';
import '../builder/constructor_builder.dart';
import '../builder/declaration_builders.dart';
import '../builder/formal_parameter_builder.dart';
import '../builder/function_builder.dart';
import '../builder/library_builder.dart';
import '../builder/member_builder.dart';
import '../builder/metadata_builder.dart';
import '../builder/omitted_type_builder.dart';
import '../builder/type_builder.dart';
import '../base/identifiers.dart';
import '../kernel/internal_ast.dart' show VariableDeclarationImpl;
import '../kernel/kernel_helper.dart';
import '../base/messages.dart'
show
messagePatchDeclarationMismatch,
messagePatchDeclarationOrigin,
messagePatchNonExternal,
noLength,
templateRequiredNamedParameterHasDefaultValueError;
import '../base/modifier.dart';
import '../base/scope.dart';
import 'source_loader.dart' show SourceLoader;
import '../type_inference/type_inference_engine.dart'
show IncludesTypeParametersNonCovariantly;
import 'source_builder_mixins.dart';
import 'source_extension_type_declaration_builder.dart';
import 'source_loader.dart' show SourceLoader;
import 'source_member_builder.dart';

abstract class SourceFunctionBuilder
Expand Down Expand Up @@ -155,11 +154,11 @@ abstract class SourceFunctionBuilderImpl extends SourceMemberBuilderImpl
this.name,
this.typeVariables,
this.formals,
LibraryBuilder compilationUnit,
Builder parent,
Uri fileUri,
int charOffset,
this.nativeMethodName)
: super(compilationUnit, fileUri, charOffset) {
: super(parent, fileUri, charOffset) {
returnType.registerInferredTypeListener(this);
if (formals != null) {
for (int i = 0; i < formals!.length; i++) {
Expand Down
1 change: 1 addition & 0 deletions pkg/front_end/lib/src/source/source_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,7 @@ severity: $severity
/* formals = */ null,
ProcedureKind.Method,
libraryBuilder,
null,
libraryBuilder.fileUri,
/* start char offset = */ 0,
/* char offset = */ 0,
Expand Down
18 changes: 15 additions & 3 deletions pkg/front_end/lib/src/source/source_procedure_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import 'source_member_builder.dart';

class SourceProcedureBuilder extends SourceFunctionBuilderImpl
implements ProcedureBuilder {
@override
final SourceLibraryBuilder libraryBuilder;

final int charOpenParenOffset;

AsyncMarker actualAsyncModifier = AsyncMarker.Sync;
Expand Down Expand Up @@ -84,7 +87,8 @@ class SourceProcedureBuilder extends SourceFunctionBuilderImpl
List<NominalVariableBuilder>? typeVariables,
List<FormalParameterBuilder>? formals,
this.kind,
SourceLibraryBuilder libraryBuilder,
this.libraryBuilder,
DeclarationBuilder? declarationBuilder,
Uri fileUri,
int startCharOffset,
int charOffset,
Expand All @@ -102,8 +106,16 @@ class SourceProcedureBuilder extends SourceFunctionBuilderImpl
this.isExtensionTypeInstanceMember =
nameScheme.isInstanceMember && nameScheme.isExtensionTypeMember,
_memberName = nameScheme.getDeclaredName(name),
super(metadata, modifiers, name, typeVariables, formals, libraryBuilder,
fileUri, charOffset, nativeMethodName) {
super(
metadata,
modifiers,
name,
typeVariables,
formals,
declarationBuilder ?? libraryBuilder,
fileUri,
charOffset,
nativeMethodName) {
_procedure = new Procedure(
dummyName,
isExtensionInstanceMember || isExtensionTypeInstanceMember
Expand Down
26 changes: 26 additions & 0 deletions pkg/front_end/lib/src/source/type_parameter_scope_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'source_extension_type_declaration_builder.dart';
import 'source_field_builder.dart';
import 'source_library_builder.dart';
import 'source_loader.dart';
import 'source_procedure_builder.dart';
import 'source_type_alias_builder.dart';

sealed class _Added {
Expand Down Expand Up @@ -383,6 +384,30 @@ class _AddedFragment implements _Added {
fragment.builder = fieldBuilder;
builders.add(new _AddBuilder(fragment.name, fieldBuilder,
fragment.fileUri, fragment.charOffset));
case MethodFragment():
SourceProcedureBuilder procedureBuilder = new SourceProcedureBuilder(
fragment.metadata,
fragment.modifiers,
fragment.returnType,
fragment.name,
fragment.typeParameters,
fragment.formals,
fragment.kind,
enclosingLibraryBuilder,
declarationBuilder,
fragment.fileUri,
fragment.startCharOffset,
fragment.charOffset,
fragment.charOpenParenOffset,
fragment.charEndOffset,
fragment.procedureReference,
fragment.tearOffReference,
fragment.asyncModifier,
fragment.nameScheme,
nativeMethodName: fragment.nativeMethodName);
fragment.builder = procedureBuilder;
builders.add(new _AddBuilder(fragment.name, procedureBuilder,
fragment.fileUri, fragment.charOffset));
}
}
}
Expand All @@ -394,6 +419,7 @@ class LibraryNameSpaceBuilder {

List<_Added> _added = [];

// Coverage-ignore(suite): Not run.
void addBuilder(
String name, Builder declaration, Uri fileUri, int charOffset) {
_added.add(new _AddedBuilder(
Expand Down
Loading

0 comments on commit df4e73c

Please sign in to comment.