Skip to content

Commit d05ea2f

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Start moving toward element builders.
Change-Id: Iad5abe08363a658f48a24d7ba8103c5102314508 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391497 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent eafb6b7 commit d05ea2f

File tree

4 files changed

+106
-59
lines changed

4 files changed

+106
-59
lines changed

pkg/analyzer/lib/src/summary2/augmentation.dart

Lines changed: 68 additions & 43 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:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/dart/element/element2.dart';
67
import 'package:analyzer/src/dart/element/element.dart';
78
import 'package:analyzer/src/dart/element/member.dart';
89
import 'package:analyzer/src/dart/element/type.dart';
@@ -12,12 +13,10 @@ import 'package:analyzer/src/utilities/extensions/element.dart';
1213
import 'package:analyzer/src/utilities/extensions/string.dart';
1314

1415
class AugmentedClassDeclarationBuilder
15-
extends AugmentedInstanceDeclarationBuilder {
16-
final ClassElementImpl firstFragment;
17-
16+
extends AugmentedInstanceDeclarationBuilder<ClassElementImpl> {
1817
AugmentedClassDeclarationBuilder({
18+
required super.firstFragment,
1919
required MaybeAugmentedClassElementMixin element,
20-
required this.firstFragment,
2120
}) {
2221
firstFragment.augmentedInternal = element;
2322
addFields(firstFragment.fields);
@@ -27,6 +26,9 @@ class AugmentedClassDeclarationBuilder
2726
}
2827

2928
void augment(ClassElementImpl fragment) {
29+
lastFragment.augmentation = fragment;
30+
lastFragment = fragment;
31+
3032
var element = _ensureAugmented(firstFragment);
3133
fragment.augmentedInternal = firstFragment.augmentedInternal;
3234

@@ -39,21 +41,19 @@ class AugmentedClassDeclarationBuilder
3941
}
4042

4143
class AugmentedEnumDeclarationBuilder
42-
extends AugmentedInstanceDeclarationBuilder {
43-
final EnumElementImpl declaration;
44-
44+
extends AugmentedInstanceDeclarationBuilder<EnumElementImpl> {
4545
AugmentedEnumDeclarationBuilder({
46-
required this.declaration,
46+
required super.firstFragment,
4747
}) {
48-
addFields(declaration.fields);
49-
addConstructors(declaration.constructors);
50-
addAccessors(declaration.accessors);
51-
addMethods(declaration.methods);
48+
addFields(firstFragment.fields);
49+
addConstructors(firstFragment.constructors);
50+
addAccessors(firstFragment.accessors);
51+
addMethods(firstFragment.methods);
5252
}
5353

5454
void augment(EnumElementImpl element) {
55-
var augmented = _ensureAugmented(declaration);
56-
element.augmentedInternal = declaration.augmentedInternal;
55+
var augmented = _ensureAugmented(firstFragment);
56+
element.augmentedInternal = firstFragment.augmentedInternal;
5757

5858
addFields(element.fields);
5959
addConstructors(element.constructors);
@@ -64,20 +64,18 @@ class AugmentedEnumDeclarationBuilder
6464
}
6565

6666
class AugmentedExtensionDeclarationBuilder
67-
extends AugmentedInstanceDeclarationBuilder {
68-
final ExtensionElementImpl declaration;
69-
67+
extends AugmentedInstanceDeclarationBuilder<ExtensionElementImpl> {
7068
AugmentedExtensionDeclarationBuilder({
71-
required this.declaration,
69+
required super.firstFragment,
7270
}) {
73-
addFields(declaration.fields);
74-
addAccessors(declaration.accessors);
75-
addMethods(declaration.methods);
71+
addFields(firstFragment.fields);
72+
addAccessors(firstFragment.accessors);
73+
addMethods(firstFragment.methods);
7674
}
7775

7876
void augment(ExtensionElementImpl element) {
79-
var augmented = _ensureAugmented(declaration);
80-
element.augmentedInternal = declaration.augmentedInternal;
77+
var augmented = _ensureAugmented(firstFragment);
78+
element.augmentedInternal = firstFragment.augmentedInternal;
8179

8280
addFields(element.fields);
8381
addAccessors(element.accessors);
@@ -87,21 +85,19 @@ class AugmentedExtensionDeclarationBuilder
8785
}
8886

8987
class AugmentedExtensionTypeDeclarationBuilder
90-
extends AugmentedInstanceDeclarationBuilder {
91-
final ExtensionTypeElementImpl declaration;
92-
88+
extends AugmentedInstanceDeclarationBuilder<ExtensionTypeElementImpl> {
9389
AugmentedExtensionTypeDeclarationBuilder({
94-
required this.declaration,
90+
required super.firstFragment,
9591
}) {
96-
addFields(declaration.fields);
97-
addConstructors(declaration.constructors);
98-
addAccessors(declaration.accessors);
99-
addMethods(declaration.methods);
92+
addFields(firstFragment.fields);
93+
addConstructors(firstFragment.constructors);
94+
addAccessors(firstFragment.accessors);
95+
addMethods(firstFragment.methods);
10096
}
10197

10298
void augment(ExtensionTypeElementImpl element) {
103-
var augmented = _ensureAugmented(declaration);
104-
element.augmentedInternal = declaration.augmentedInternal;
99+
var augmented = _ensureAugmented(firstFragment);
100+
element.augmentedInternal = firstFragment.augmentedInternal;
105101

106102
addFields(element.fields);
107103
addConstructors(element.constructors);
@@ -111,13 +107,18 @@ class AugmentedExtensionTypeDeclarationBuilder
111107
}
112108
}
113109

114-
abstract class AugmentedInstanceDeclarationBuilder {
110+
abstract class AugmentedInstanceDeclarationBuilder<
111+
F extends InstanceElementImpl> extends FragmentedElementBuilder<F> {
115112
final Map<String, FieldElementImpl> fields = {};
116113
final Map<String, ConstructorElementImpl> constructors = {};
117114
final Map<String, PropertyAccessorElementImpl> getters = {};
118115
final Map<String, PropertyAccessorElementImpl> setters = {};
119116
final Map<String, MethodElementImpl> methods = {};
120117

118+
AugmentedInstanceDeclarationBuilder({
119+
required super.firstFragment,
120+
});
121+
121122
void addAccessors(List<PropertyAccessorElementImpl> elements) {
122123
for (var element in elements) {
123124
var name = element.name;
@@ -319,20 +320,18 @@ abstract class AugmentedInstanceDeclarationBuilder {
319320
}
320321

321322
class AugmentedMixinDeclarationBuilder
322-
extends AugmentedInstanceDeclarationBuilder {
323-
final MixinElementImpl declaration;
324-
323+
extends AugmentedInstanceDeclarationBuilder<MixinElementImpl> {
325324
AugmentedMixinDeclarationBuilder({
326-
required this.declaration,
325+
required super.firstFragment,
327326
}) {
328-
addFields(declaration.fields);
329-
addAccessors(declaration.accessors);
330-
addMethods(declaration.methods);
327+
addFields(firstFragment.fields);
328+
addAccessors(firstFragment.accessors);
329+
addMethods(firstFragment.methods);
331330
}
332331

333332
void augment(MixinElementImpl element) {
334-
var augmented = _ensureAugmented(declaration);
335-
element.augmentedInternal = declaration.augmentedInternal;
333+
var augmented = _ensureAugmented(firstFragment);
334+
element.augmentedInternal = firstFragment.augmentedInternal;
336335

337336
addFields(element.fields);
338337
addAccessors(element.accessors);
@@ -402,6 +401,32 @@ class AugmentedTopVariablesBuilder {
402401
}
403402
}
404403

404+
/// A builder for top-level fragmented elements, e.g. classes.
405+
class FragmentedElementBuilder<F extends Fragment> {
406+
final F firstFragment;
407+
F lastFragment;
408+
409+
FragmentedElementBuilder({
410+
required this.firstFragment,
411+
}) : lastFragment = firstFragment;
412+
413+
/// If [fragment] is an augmentation, set its previous fragment to
414+
/// [lastFragment].
415+
///
416+
/// We invoke this method on any [FragmentedElementBuilder] associated with
417+
/// the name of [fragment], even if it is not a correct builder for this
418+
/// [fragment]. So, the [lastFragment] might have a wrong type, but we still
419+
/// want to remember it for generating the corresponding diagnostic.
420+
void setPreviousFor(AugmentableElement fragment) {
421+
if (fragment.isAugmentation) {
422+
// TODO(scheglov): hopefully the type check can be removed in the future.
423+
if (lastFragment case ElementImpl lastFragment) {
424+
fragment.augmentationTargetAny = lastFragment;
425+
}
426+
}
427+
}
428+
}
429+
405430
extension<T extends ExecutableElement> on List<T> {
406431
Iterable<T> get notAugmented {
407432
return where((e) => e.augmentation == null);

pkg/analyzer/lib/src/summary2/element_builder.dart

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,29 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
140140
node.withClause?.accept(this);
141141
node.implementsClause?.accept(this);
142142

143-
_libraryBuilder.updateAugmentationTarget(name, element);
143+
// TODO(scheglov): remove it eventually
144+
_libraryBuilder.updateAugmentationTarget0(name, element);
145+
146+
var augmentedBuilder = _libraryBuilder.getAugmentedBuilder(name);
147+
augmentedBuilder?.setPreviousFor(element);
144148

145149
// If the fragment is an augmentation, and the corresponding builder
146150
// has correct type, add the fragment to the builder. Otherwise, create
147151
// a new builder.
148-
var augmentedBuilder = _libraryBuilder.getAugmentedBuilder(name);
149-
if (element.augmentationTarget != null &&
152+
if (element.isAugmentation &&
150153
augmentedBuilder is AugmentedClassDeclarationBuilder) {
151154
augmentedBuilder.augment(element);
152155
} else {
153-
var containerRef = _libraryBuilder.reference.getChild('@class');
156+
var libraryRef = _libraryBuilder.reference;
157+
var containerRef = libraryRef.getChild('@class');
154158
var elementReference = containerRef.addChild(refName);
155159
var element2 = NotAugmentedClassElementImpl(elementReference, element);
156160

157161
_libraryBuilder.putAugmentedBuilder(
158162
name,
159163
AugmentedClassDeclarationBuilder(
160-
element: element2,
161164
firstFragment: element,
165+
element: element2,
162166
),
163167
);
164168
}
@@ -479,7 +483,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
479483
_libraryBuilder.putAugmentedBuilder(
480484
name,
481485
AugmentedEnumDeclarationBuilder(
482-
declaration: element,
486+
firstFragment: element,
483487
),
484488
);
485489
}
@@ -548,7 +552,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
548552
_libraryBuilder.putAugmentedBuilder(
549553
name,
550554
AugmentedExtensionDeclarationBuilder(
551-
declaration: element,
555+
firstFragment: element,
552556
),
553557
);
554558
}
@@ -622,7 +626,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
622626
_libraryBuilder.putAugmentedBuilder(
623627
name,
624628
AugmentedExtensionTypeDeclarationBuilder(
625-
declaration: element,
629+
firstFragment: element,
626630
),
627631
);
628632
}
@@ -770,6 +774,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
770774
}
771775

772776
_libraryBuilder.topVariables.addAccessor(element);
777+
_libraryBuilder.putAugmentedBuilder(
778+
name,
779+
FragmentedElementBuilder(firstFragment: element),
780+
);
773781
} else if (node.isSetter) {
774782
var element = PropertyAccessorElementImpl(name, nameOffset);
775783
element.name2 = fragmentName;
@@ -785,6 +793,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
785793
}
786794

787795
_libraryBuilder.topVariables.addAccessor(element);
796+
_libraryBuilder.putAugmentedBuilder(
797+
'$name=',
798+
FragmentedElementBuilder(firstFragment: element),
799+
);
788800
} else {
789801
var element = FunctionElementImpl(name, nameOffset);
790802
element.name2 = fragmentName;
@@ -794,6 +806,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
794806
executableElement = element;
795807

796808
_libraryBuilder.updateAugmentationTarget(name, element);
809+
_libraryBuilder.putAugmentedBuilder(
810+
name,
811+
FragmentedElementBuilder(firstFragment: element),
812+
);
797813
}
798814

799815
executableElement.hasImplicitReturnType = node.returnType == null;
@@ -1133,7 +1149,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
11331149
_libraryBuilder.putAugmentedBuilder(
11341150
name,
11351151
AugmentedMixinDeclarationBuilder(
1136-
declaration: element,
1152+
firstFragment: element,
11371153
),
11381154
);
11391155
}

pkg/analyzer/lib/src/summary2/library_builder.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ class LibraryBuilder with MacroApplicationsContainer {
8282
Map.identity();
8383

8484
/// The top-level elements that can be augmented.
85-
final Map<String, AugmentedInstanceDeclarationBuilder> _augmentedBuilders =
86-
{};
85+
final Map<String, FragmentedElementBuilder> _augmentedBuilders = {};
8786

8887
/// The top-level variables and accessors that can be augmented.
8988
late final AugmentedTopVariablesBuilder topVariables =
@@ -472,7 +471,7 @@ class LibraryBuilder with MacroApplicationsContainer {
472471
}
473472
}
474473

475-
AugmentedInstanceDeclarationBuilder? getAugmentedBuilder(String name) {
474+
FragmentedElementBuilder? getAugmentedBuilder(String name) {
476475
return _augmentedBuilders[name];
477476
}
478477

@@ -654,9 +653,9 @@ class LibraryBuilder with MacroApplicationsContainer {
654653

655654
void putAugmentedBuilder(
656655
String name,
657-
AugmentedInstanceDeclarationBuilder element,
656+
FragmentedElementBuilder builder,
658657
) {
659-
_augmentedBuilders[name] = element;
658+
_augmentedBuilders[name] = builder;
660659
}
661660

662661
void replaceConstFieldsIfNoConstConstructor() {
@@ -797,6 +796,14 @@ class LibraryBuilder with MacroApplicationsContainer {
797796
_augmentationTargets[name] = augmentation;
798797
}
799798

799+
// TODO(scheglov): Remove this method when we use only builders.
800+
void updateAugmentationTarget0<T extends ElementImpl>(
801+
String name,
802+
AugmentableElement<T> augmentation,
803+
) {
804+
_augmentationTargets[name] = augmentation;
805+
}
806+
800807
/// Updates the element of the macro augmentation.
801808
void updateInputMacroAugmentation() {
802809
if (inputMacroPartInclude case var import?) {

pkg/analyzer/test/src/diagnostics/augmentation_of_different_declaration_kind_test.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,7 @@ part of 'a.dart';
652652
653653
augment class foo {}
654654
''', [
655-
error(CompileTimeErrorCode.AUGMENTATION_OF_DIFFERENT_DECLARATION_KIND, 19,
656-
7),
655+
error(CompileTimeErrorCode.AUGMENTATION_WITHOUT_DECLARATION, 19, 7),
657656
]);
658657
}
659658
}

0 commit comments

Comments
 (0)