33// BSD-style license that can be found in the LICENSE file.
44
55import 'package:kernel/ast.dart' ;
6+ import 'package:kernel/class_hierarchy.dart' ;
67
78import '../base/common.dart' ;
89import '../base/modifiers.dart' ;
@@ -19,7 +20,9 @@ import '../codes/cfe_codes.dart'
1920 messagePatchDeclarationMismatch,
2021 messagePatchDeclarationOrigin,
2122 noLength;
23+ import '../fragment/fragment.dart' ;
2224import '../kernel/body_builder_context.dart' ;
25+ import '../kernel/kernel_helper.dart' ;
2326import 'name_scheme.dart' ;
2427import 'source_builder_mixins.dart' ;
2528import '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 (
0 commit comments