Skip to content

Commit 5333f30

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Add introductory to SourceExtensionBuilder
Change-Id: I34966f96110b6101dbdf96571af12eba2b0e39d1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389460 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
1 parent b7daafa commit 5333f30

24 files changed

+414
-70
lines changed

pkg/front_end/lib/src/base/scope.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ extension on Builder {
14741474
// TODO(johnniwinther): Handle all cases here.
14751475
}
14761476

1477-
bool _hasPatchAnnotation(List<MetadataBuilder>? metadata) {
1477+
bool _hasPatchAnnotation(Iterable<MetadataBuilder>? metadata) {
14781478
if (metadata == null) {
14791479
return false;
14801480
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ abstract class ExtensionBuilder implements DeclarationBuilder {
1313
/// The type of the on-clause of the extension declaration.
1414
TypeBuilder get onType;
1515

16+
/// Reference for the extension built by this builder.
17+
Reference get reference;
18+
1619
/// Return the [Extension] built by this builder.
1720
Extension get extension;
1821

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class DillExtensionBuilder extends ExtensionBuilderImpl
8787
}
8888
}
8989

90+
@override
91+
Reference get reference => extension.reference;
92+
9093
@override
9194
int get charOffset => extension.fileOffset;
9295

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ class DillLibraryBuilder extends LibraryBuilderImpl {
473473
reference == declaration.reference) ||
474474
(declaration is MemberBuilder && node == declaration.member) ||
475475
(declaration is ExtensionBuilder &&
476-
node == declaration.extension) ||
476+
reference == declaration.reference) ||
477477
(declaration is ExtensionTypeDeclarationBuilder &&
478478
node == declaration.extensionTypeDeclaration),
479479
"Unexpected declaration ${declaration} (${declaration.runtimeType}) "

pkg/front_end/lib/src/source/source_builder_mixins.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import '../builder/builder_mixins.dart';
1414
import '../builder/declaration_builders.dart';
1515
import '../builder/library_builder.dart';
1616
import '../builder/member_builder.dart';
17-
import '../builder/metadata_builder.dart';
1817
import '../builder/name_iterator.dart';
1918
import '../builder/procedure_builder.dart';
2019
import '../builder/type_builder.dart';
@@ -39,8 +38,6 @@ mixin SourceDeclarationBuilderMixin
3938
@override
4039
Uri get fileUri;
4140

42-
List<MetadataBuilder>? get metadata;
43-
4441
/// Returns the [Annotatable] node that holds the annotations declared on
4542
/// this declaration or its augmentations.
4643
Annotatable get annotatable;
@@ -165,16 +162,6 @@ mixin SourceDeclarationBuilderMixin
165162

166163
void buildOutlineExpressions(ClassHierarchy classHierarchy,
167164
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
168-
MetadataBuilder.buildAnnotations(
169-
annotatable,
170-
metadata,
171-
createBodyBuilderContext(
172-
inOutlineBuildingPhase: true,
173-
inMetadata: true,
174-
inConstFields: false),
175-
libraryBuilder,
176-
fileUri,
177-
libraryBuilder.scope);
178165
if (typeParameters != null) {
179166
for (int i = 0; i < typeParameters!.length; i++) {
180167
typeParameters![i].buildOutlineExpressions(

pkg/front_end/lib/src/source/source_extension_builder.dart

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:kernel/ast.dart';
6+
import 'package:kernel/class_hierarchy.dart';
67

78
import '../base/common.dart';
89
import '../base/modifiers.dart';
@@ -19,7 +20,9 @@ import '../codes/cfe_codes.dart'
1920
messagePatchDeclarationMismatch,
2021
messagePatchDeclarationOrigin,
2122
noLength;
23+
import '../fragment/fragment.dart';
2224
import '../kernel/body_builder_context.dart';
25+
import '../kernel/kernel_helper.dart';
2326
import 'name_scheme.dart';
2427
import 'source_builder_mixins.dart';
2528
import 'source_library_builder.dart';
@@ -31,18 +34,14 @@ class SourceExtensionBuilder extends ExtensionBuilderImpl
3134
@override
3235
final SourceLibraryBuilder parent;
3336

34-
@override
35-
final int charOffset;
37+
final int _nameOffset;
3638

3739
@override
3840
final Uri fileUri;
3941

40-
@override
41-
final List<MetadataBuilder>? metadata;
42-
4342
final Modifiers _modifiers;
4443

45-
final Extension _extension;
44+
late final Extension _extension;
4645

4746
SourceExtensionBuilder? _origin;
4847
SourceExtensionBuilder? augmentationForTesting;
@@ -68,35 +67,54 @@ class SourceExtensionBuilder extends ExtensionBuilderImpl
6867

6968
final ExtensionName extensionName;
7069

70+
final Reference _reference;
71+
72+
/// The `extension` declaration that introduces this extension. Subsequent
73+
/// extensions of the same name must be augmentations.
74+
// TODO(johnniwinther): Add [_augmentations] field.
75+
final ExtensionFragment _introductory;
76+
7177
SourceExtensionBuilder(
72-
{required this.metadata,
73-
required Modifiers modifiers,
74-
required this.extensionName,
75-
required this.typeParameters,
76-
required this.onType,
77-
required this.typeParameterScope,
78-
required DeclarationNameSpaceBuilder nameSpaceBuilder,
79-
required SourceLibraryBuilder enclosingLibraryBuilder,
78+
{required SourceLibraryBuilder enclosingLibraryBuilder,
8079
required this.fileUri,
8180
required int startOffset,
8281
required int nameOffset,
8382
required int endOffset,
83+
required ExtensionFragment fragment,
8484
required Reference? reference})
85-
: charOffset = nameOffset,
85+
: _introductory = fragment,
86+
_reference = reference ?? new Reference(),
87+
_nameOffset = nameOffset,
8688
parent = enclosingLibraryBuilder,
87-
_modifiers = modifiers,
88-
_extension = new Extension(
89-
name: extensionName.name,
90-
fileUri: fileUri,
91-
typeParameters: NominalVariableBuilder.typeParametersFromBuilders(
92-
typeParameters),
93-
reference: reference)
94-
..isUnnamedExtension = extensionName.isUnnamedExtension
95-
..fileOffset = nameOffset,
96-
_nameSpaceBuilder = nameSpaceBuilder {
89+
_modifiers = fragment.modifiers,
90+
extensionName = fragment.extensionName,
91+
typeParameters = fragment.typeParameters,
92+
typeParameterScope = fragment.typeParameterScope,
93+
onType = fragment.onType,
94+
_nameSpaceBuilder = fragment.toDeclarationNameSpaceBuilder() {
95+
_introductory.builder = this;
96+
_introductory.bodyScope.declarationBuilder = this;
97+
98+
// TODO(johnniwinther): Move this to the [build] once augmentations are
99+
// handled through fragments.
100+
_extension = new Extension(
101+
name: extensionName.name,
102+
fileUri: fileUri,
103+
typeParameters:
104+
NominalVariableBuilder.typeParametersFromBuilders(typeParameters),
105+
reference: _reference)
106+
..isUnnamedExtension = extensionName.isUnnamedExtension
107+
..fileOffset = _nameOffset;
97108
extensionName.attachExtension(_extension);
98109
}
99110

111+
// TODO(johnniwinther): Avoid exposing this. Annotations for macros and
112+
// patches should be computing from within the builder.
113+
Iterable<MetadataBuilder>? get metadata => _introductory.metadata;
114+
115+
@override
116+
int get charOffset => _nameOffset;
117+
100118
@override
101119
String get name => extensionName.name;
102120

@@ -162,11 +180,12 @@ class SourceExtensionBuilder extends ExtensionBuilderImpl
162180
: throw new UnimplementedError("SourceExtensionBuilder.mergedScope");
163181

164182
@override
165-
Extension get extension => isAugmenting
166-
?
167-
// Coverage-ignore(suite): Not run.
168-
origin._extension
169-
: _extension;
183+
Reference get reference => _reference;
184+
185+
@override
186+
Extension get extension {
187+
return isAugmenting ? origin.extension : _extension;
188+
}
170189

171190
@override
172191
BodyBuilderContext createBodyBuilderContext(
@@ -195,10 +214,26 @@ class SourceExtensionBuilder extends ExtensionBuilderImpl
195214
_extension.onType = onType.build(libraryBuilder, TypeUse.extensionOnType);
196215

197216
buildInternal(coreLibrary, addMembersToLibrary: addMembersToLibrary);
198-
199217
return _extension;
200218
}
201219

220+
@override
221+
void buildOutlineExpressions(ClassHierarchy classHierarchy,
222+
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
223+
MetadataBuilder.buildAnnotations(
224+
annotatable,
225+
_introductory.metadata,
226+
createBodyBuilderContext(
227+
inOutlineBuildingPhase: true,
228+
inMetadata: true,
229+
inConstFields: false),
230+
libraryBuilder,
231+
_introductory.fileUri,
232+
libraryBuilder.scope);
233+
234+
super.buildOutlineExpressions(classHierarchy, delayedDefaultValueCloners);
235+
}
236+
202237
@override
203238
// Coverage-ignore(suite): Not run.
204239
void addMemberInternal(SourceMemberBuilder memberBuilder,
@@ -263,11 +298,11 @@ class SourceExtensionBuilder extends ExtensionBuilderImpl
263298
}
264299

265300
@override
266-
// Coverage-ignore(suite): Not run.
267301
void applyAugmentation(Builder augmentation) {
268302
if (augmentation is SourceExtensionBuilder) {
269303
augmentation._origin = this;
270304
if (retainDataForTesting) {
305+
// Coverage-ignore-block(suite): Not run.
271306
augmentationForTesting = augmentation;
272307
}
273308
// TODO(johnniwinther): Check that type parameters and on-type match
@@ -280,14 +315,17 @@ class SourceExtensionBuilder extends ExtensionBuilderImpl
280315
member.applyAugmentation(memberAugmentation);
281316
}
282317
});
283-
nameSpace.forEachLocalSetter((String name, Builder member) {
318+
nameSpace.forEachLocalSetter(
319+
// Coverage-ignore(suite): Not run.
320+
(String name, Builder member) {
284321
Builder? memberAugmentation =
285322
augmentation.nameSpace.lookupLocalMember(name, setter: true);
286323
if (memberAugmentation != null) {
287324
member.applyAugmentation(memberAugmentation);
288325
}
289326
});
290327
} else {
328+
// Coverage-ignore-block(suite): Not run.
291329
libraryBuilder.addProblem(messagePatchDeclarationMismatch,
292330
augmentation.charOffset, noLength, augmentation.fileUri, context: [
293331
messagePatchDeclarationOrigin.withLocation(

pkg/front_end/lib/src/source/source_extension_type_declaration_builder.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class SourceExtensionTypeDeclarationBuilder
6161
@override
6262
final Uri fileUri;
6363

64-
@override
64+
// TODO(johnniwinther): Avoid exposing this. Annotations for macros and
65+
// patches should be computing from within the builder.
6566
final List<MetadataBuilder>? metadata;
6667

6768
final Modifiers _modifiers;
@@ -722,6 +723,17 @@ class SourceExtensionTypeDeclarationBuilder
722723
@override
723724
void buildOutlineExpressions(ClassHierarchy classHierarchy,
724725
List<DelayedDefaultValueCloner> delayedDefaultValueCloners) {
726+
MetadataBuilder.buildAnnotations(
727+
annotatable,
728+
metadata,
729+
createBodyBuilderContext(
730+
inOutlineBuildingPhase: true,
731+
inMetadata: true,
732+
inConstFields: false),
733+
libraryBuilder,
734+
fileUri,
735+
libraryBuilder.scope);
736+
725737
super.buildOutlineExpressions(classHierarchy, delayedDefaultValueCloners);
726738

727739
Iterator<SourceMemberBuilder> iterator =

pkg/front_end/lib/src/source/source_library_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {
763763
case TypeAliasBuilder():
764764
library.additionalExports.add(builder.reference);
765765
case ExtensionBuilder():
766-
library.additionalExports.add(builder.extension.reference);
766+
library.additionalExports.add(builder.reference);
767767
case ExtensionTypeDeclarationBuilder():
768768
library.additionalExports
769769
.add(builder.extensionTypeDeclaration.reference);

pkg/front_end/lib/src/source/source_type_alias_builder.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ class SourceTypeAliasBuilder extends TypeAliasBuilderImpl {
5252
@override
5353
Map<Name, Procedure>? tearOffs;
5454

55-
TypedefFragment _introductory;
55+
/// The `typedef` declaration that introduces this typedef. Subsequent
56+
/// typedefs of the same name must be augmentations.
57+
// TODO(johnniwinther): Add [_augmentations] field.
58+
final TypedefFragment _introductory;
5659

5760
SourceTypeAliasBuilder(
5861
{required this.name,

pkg/front_end/lib/src/source/type_parameter_scope_builder.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,13 @@ class _Added {
268268
reference = indexedLibrary?.lookupExtension(fragment.name);
269269
}
270270
SourceExtensionBuilder extensionBuilder = new SourceExtensionBuilder(
271-
metadata: fragment.metadata,
272-
modifiers: fragment.modifiers,
273-
extensionName: fragment.extensionName,
274-
typeParameters: fragment.typeParameters,
275-
onType: fragment.onType,
276-
typeParameterScope: fragment.typeParameterScope,
277-
nameSpaceBuilder: fragment.toDeclarationNameSpaceBuilder(),
278271
enclosingLibraryBuilder: enclosingLibraryBuilder,
279272
fileUri: fragment.fileUri,
280273
startOffset: fragment.startOffset,
281274
nameOffset: fragment.nameOffset,
282275
endOffset: fragment.endOffset,
276+
fragment: fragment,
283277
reference: reference);
284-
fragment.builder = extensionBuilder;
285-
fragment.bodyScope.declarationBuilder = extensionBuilder;
286278
builders.add(new _AddBuilder(fragment.name, extensionBuilder,
287279
fragment.fileUri, fragment.fileOffset));
288280
if (reference != null) {

0 commit comments

Comments
 (0)