Skip to content

Commit 688fdb0

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Support for property accessors in augmentations.
Change-Id: I06e68030f3f1c0b9cb86df2abd7ba9b90d93f5e5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/314585 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 3284c68 commit 688fdb0

12 files changed

+2044
-251
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import 'package:analyzer/src/utilities/uri_cache.dart';
8787
/// TODO(scheglov) Clean up the list of implicitly analyzed files.
8888
class AnalysisDriver implements AnalysisDriverGeneric {
8989
/// The version of data format, should be incremented on every format change.
90-
static const int DATA_VERSION = 289;
90+
static const int DATA_VERSION = 290;
9191

9292
/// The number of exception contexts allowed to write. Once this field is
9393
/// zero, we stop writing any new exception contexts in this process.

pkg/analyzer/lib/src/dart/element/display_string_builder.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ class ElementDisplayStringBuilder {
9595
}
9696

9797
void writeExecutableElement(ExecutableElement element, String name) {
98+
if (element.isAugmentation) {
99+
_write('augment ');
100+
}
101+
98102
_writeType(element.returnType2);
99103
_write(' ');
100104

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,17 @@ class AugmentedInlineClassElementImpl extends AugmentedNamedInstanceElementImpl
117117
abstract class AugmentedInstanceElementImpl
118118
implements AugmentedInstanceElement {
119119
@override
120-
List<MethodElement> methods = [];
120+
List<FieldElement> fields = [];
121121

122122
@override
123-
// TODO: implement accessors
124-
List<PropertyAccessorElement> get accessors => throw UnimplementedError();
123+
List<PropertyAccessorElement> accessors = [];
125124

126125
@override
127-
// TODO: implement declaration
128-
InstanceElement get declaration => throw UnimplementedError();
126+
List<MethodElement> methods = [];
129127

130128
@override
131-
// TODO: implement fields
132-
List<FieldElement> get fields => throw UnimplementedError();
129+
// TODO: implement declaration
130+
InstanceElement get declaration => throw UnimplementedError();
133131

134132
@override
135133
// TODO: implement metadata
@@ -6063,36 +6061,50 @@ class PrefixElementImpl extends _ExistingElementImpl implements PrefixElement {
60636061
/// A concrete implementation of a [PropertyAccessorElement].
60646062
class PropertyAccessorElementImpl extends ExecutableElementImpl
60656063
implements PropertyAccessorElement {
6066-
/// The variable associated with this accessor.
6067-
@override
6068-
late PropertyInducingElementImpl variable;
6064+
late PropertyInducingElementImpl _variable;
60696065

60706066
/// If this method is a synthetic element which is based on another method
60716067
/// with some modifications (such as making some parameters covariant),
60726068
/// this field contains the base method.
60736069
PropertyAccessorElement? prototype;
60746070

6075-
@override
6076-
PropertyAccessorElementImpl? augmentationTarget;
6071+
PropertyAccessorElementImpl? _augmentation;
6072+
6073+
PropertyAccessorElementImpl? _augmentationTarget;
60776074

60786075
/// Initialize a newly created property accessor element to have the given
60796076
/// [name] and [offset].
60806077
PropertyAccessorElementImpl(super.name, super.offset);
60816078

60826079
/// Initialize a newly created synthetic property accessor element to be
60836080
/// associated with the given [variable].
6084-
PropertyAccessorElementImpl.forVariable(this.variable, {Reference? reference})
6085-
: super(variable.name, -1, reference: reference) {
6081+
PropertyAccessorElementImpl.forVariable(this._variable,
6082+
{Reference? reference})
6083+
: super(_variable.name, -1, reference: reference) {
60866084
isAbstract = variable is FieldElementImpl &&
60876085
(variable as FieldElementImpl).isAbstract;
60886086
isStatic = variable.isStatic;
60896087
isSynthetic = true;
60906088
}
60916089

60926090
@override
6093-
PropertyAccessorElement? get augmentation {
6094-
// TODO(scheglov) implement
6095-
throw UnimplementedError();
6091+
PropertyAccessorElementImpl? get augmentation {
6092+
linkedData?.read(this);
6093+
return _augmentation;
6094+
}
6095+
6096+
set augmentation(PropertyAccessorElementImpl? value) {
6097+
_augmentation = value;
6098+
}
6099+
6100+
@override
6101+
PropertyAccessorElementImpl? get augmentationTarget {
6102+
linkedData?.read(this);
6103+
return _augmentationTarget;
6104+
}
6105+
6106+
set augmentationTarget(PropertyAccessorElementImpl? value) {
6107+
_augmentationTarget = value;
60966108
}
60976109

60986110
@override
@@ -6168,6 +6180,16 @@ class PropertyAccessorElementImpl extends ExecutableElementImpl
61686180
return super.name;
61696181
}
61706182

6183+
@override
6184+
PropertyInducingElementImpl get variable {
6185+
linkedData?.read(this);
6186+
return _variable;
6187+
}
6188+
6189+
set variable(PropertyInducingElementImpl value) {
6190+
_variable = value;
6191+
}
6192+
61716193
@override
61726194
T? accept<T>(ElementVisitor<T> visitor) =>
61736195
visitor.visitPropertyAccessorElement(this);

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class ClassElementLinkedData extends ElementLinkedData<ClassElementImpl> {
132132
element.augmentedInternal = augmented;
133133
augmented.mixins = reader._readInterfaceTypeList();
134134
augmented.interfaces = reader._readInterfaceTypeList();
135+
augmented.fields = reader.readElementList();
136+
augmented.accessors = reader.readElementList();
135137
augmented.methods = reader.readElementList();
136138
}
137139
}
@@ -554,6 +556,8 @@ class LibraryReader {
554556
_reader.readUInt30List(),
555557
);
556558

559+
final accessorAugmentationsOffset = _reader.readUInt30();
560+
557561
libraryElement.linkedData = LibraryElementLinkedData(
558562
reference: _reference,
559563
libraryReader: this,
@@ -566,6 +570,8 @@ class LibraryReader {
566570
InformativeDataApplier(_elementFactory, _unitsInformativeBytes)
567571
.applyTo(libraryElement);
568572

573+
_readPropertyAccessorAugmentations(accessorAugmentationsOffset);
574+
569575
return libraryElement;
570576
}
571577

@@ -1276,6 +1282,20 @@ class LibraryReader {
12761282
);
12771283
}
12781284

1285+
/// Read resolution information for property accessor augmentations,
1286+
/// during which we update `getter` and `setter` of augmented variables.
1287+
void _readPropertyAccessorAugmentations(int offset) {
1288+
final reader = ResolutionReader(
1289+
_elementFactory,
1290+
_referenceReader,
1291+
_reader.fork(_baseResolutionOffset + offset),
1292+
);
1293+
final elements = reader.readElementList<PropertyAccessorElementImpl>();
1294+
for (final element in elements) {
1295+
element.variable;
1296+
}
1297+
}
1298+
12791299
PropertyAccessorElementImpl _readPropertyAccessorElement(
12801300
CompilationUnitElementImpl unitElement,
12811301
ElementImpl classElement,
@@ -1322,6 +1342,10 @@ class LibraryReader {
13221342
);
13231343
accessors.add(accessor);
13241344

1345+
if (accessor.isAugmentation) {
1346+
continue;
1347+
}
1348+
13251349
var name = accessor.displayName;
13261350
var isGetter = accessor.isGetter;
13271351

@@ -1340,6 +1364,7 @@ class LibraryReader {
13401364
} else {
13411365
property = TopLevelVariableElementImpl(name, -1)
13421366
..enclosingElement = enclosingElement
1367+
..reference = reference
13431368
..isSynthetic = true;
13441369
reference.element ??= property;
13451370
properties.add(property);
@@ -1350,6 +1375,7 @@ class LibraryReader {
13501375
} else {
13511376
property = FieldElementImpl(name, -1)
13521377
..enclosingElement = enclosingElement
1378+
..reference = reference
13531379
..isStatic = accessor.isStatic
13541380
..isSynthetic = true;
13551381
reference.element ??= property;
@@ -1604,6 +1630,8 @@ class MixinElementLinkedData extends ElementLinkedData<MixinElementImpl> {
16041630
element.augmentedInternal = augmented;
16051631
augmented.superclassConstraints = reader._readInterfaceTypeList();
16061632
augmented.interfaces = reader._readInterfaceTypeList();
1633+
augmented.fields = reader.readElementList();
1634+
augmented.accessors = reader.readElementList();
16071635
augmented.methods = reader.readElementList();
16081636
}
16091637
}
@@ -1634,6 +1662,21 @@ class PropertyAccessorElementLinkedData
16341662
element.returnType = reader.readRequiredType();
16351663
_readFormalParameters(reader, element.parameters);
16361664

1665+
// If augmentation...
1666+
if (reader.readBool()) {
1667+
element.augmentationTarget =
1668+
reader.readElement() as PropertyAccessorElementImpl?;
1669+
final variable = reader.readElement() as PropertyInducingElementImpl;
1670+
element.variable = variable;
1671+
if (element.isGetter) {
1672+
variable.getter?.augmentation = element;
1673+
variable.getter = element;
1674+
} else {
1675+
variable.setter?.augmentation = element;
1676+
variable.setter = element;
1677+
}
1678+
}
1679+
16371680
applyConstantOffsets?.perform();
16381681
}
16391682
}
@@ -1687,12 +1730,16 @@ class ResolutionReader {
16871730

16881731
if (memberFlags == Tag.MemberLegacyWithTypeArguments ||
16891732
memberFlags == Tag.MemberWithTypeArguments) {
1690-
element as ExecutableElement;
16911733
var enclosing = element.enclosingElement2 as TypeParameterizedElement;
16921734
var typeParameters = enclosing.typeParameters;
16931735
var typeArguments = _readTypeList();
16941736
var substitution = Substitution.fromPairs(typeParameters, typeArguments);
1695-
element = ExecutableMember.from2(element, substitution);
1737+
if (element is ExecutableElement) {
1738+
element = ExecutableMember.from2(element, substitution);
1739+
} else {
1740+
element as FieldElement;
1741+
element = FieldMember.from2(element, substitution);
1742+
}
16961743
}
16971744

16981745
if (memberFlags == Tag.MemberLegacyWithoutTypeArguments ||

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class BundleWriter {
5656
/// and read them later on demand.
5757
List<int> _classMembersLengths = [];
5858

59+
/// [_writePropertyAccessorElement] adds augmentations here, so that after
60+
/// reading the library we can read them, and while doing this, update
61+
/// `getter` and `setter` of augmented variables.
62+
List<PropertyAccessorElementImpl> _accessorAugmentations = [];
63+
5964
final StringIndexer _stringIndexer = StringIndexer();
6065

6166
final List<_Library> _libraries = [];
@@ -96,7 +101,8 @@ class BundleWriter {
96101

97102
void writeLibraryElement(LibraryElementImpl libraryElement) {
98103
var libraryOffset = _sink.offset;
99-
_classMembersLengths = <int>[];
104+
_classMembersLengths = [];
105+
_accessorAugmentations = [];
100106

101107
_sink.writeUInt30(_resolutionSink.offset);
102108
_sink._writeStringReference(libraryElement.name);
@@ -115,6 +121,8 @@ class BundleWriter {
115121

116122
_sink.writeUint30List(libraryElement.nameUnion.mask);
117123

124+
_writePropertyAccessorAugmentations();
125+
118126
_libraries.add(
119127
_Library(
120128
uriStr: '${libraryElement.source.uri}',
@@ -163,6 +171,8 @@ class BundleWriter {
163171
(augmented) {
164172
_resolutionSink._writeTypeList(augmented.mixins);
165173
_resolutionSink._writeTypeList(augmented.interfaces);
174+
_resolutionSink._writeElementList(augmented.fields);
175+
_resolutionSink._writeElementList(augmented.accessors);
166176
_resolutionSink._writeElementList(augmented.methods);
167177
},
168178
);
@@ -433,6 +443,8 @@ class BundleWriter {
433443
(augmented) {
434444
_resolutionSink._writeTypeList(augmented.superclassConstraints);
435445
_resolutionSink._writeTypeList(augmented.interfaces);
446+
_resolutionSink._writeElementList(augmented.fields);
447+
_resolutionSink._writeElementList(augmented.accessors);
436448
_resolutionSink._writeElementList(augmented.methods);
437449
},
438450
);
@@ -498,6 +510,14 @@ class BundleWriter {
498510
_writeDirectiveUri(element.uri);
499511
}
500512

513+
/// Write information to update `getter` and `setter` properties of
514+
/// augmented variables to use the corresponding augmentations.
515+
void _writePropertyAccessorAugmentations() {
516+
final offset = _resolutionSink.offset;
517+
_resolutionSink._writeElementList(_accessorAugmentations);
518+
_sink.writeUInt30(offset);
519+
}
520+
501521
void _writePropertyAccessorElement(PropertyAccessorElementImpl element) {
502522
_sink.writeUInt30(_resolutionSink.offset);
503523
_sink._writeStringReference(element.displayName);
@@ -506,6 +526,12 @@ class BundleWriter {
506526
_resolutionSink._writeAnnotationList(element.metadata);
507527
_resolutionSink.writeType(element.returnType2);
508528
_writeList(element.parameters, _writeParameterElement);
529+
530+
_resolutionSink.writeIf(element.isAugmentation, () {
531+
_accessorAugmentations.add(element);
532+
_resolutionSink.writeElement(element.augmentationTarget);
533+
_resolutionSink.writeElement(element.variable);
534+
});
509535
}
510536

511537
void _writeTopLevelVariableElement(TopLevelVariableElementImpl element) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ class BufferedSink {
121121
addByte(byte);
122122
}
123123

124+
void writeIf<T extends Object>(
125+
bool condition,
126+
void Function() ifTrue,
127+
) {
128+
if (condition) {
129+
writeBool(true);
130+
ifTrue();
131+
} else {
132+
writeBool(false);
133+
}
134+
}
135+
124136
void writeIfType<T extends Object>(
125137
Object? object,
126138
void Function(T t) ifTrue,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
814814
if (node.isGetter) {
815815
var element = PropertyAccessorElementImpl(name, nameOffset);
816816
element.isAbstract = node.isAbstract;
817+
element.isAugmentation = node.augmentKeyword != null;
817818
element.isGetter = true;
818819
element.isStatic = node.isStatic;
819820

@@ -832,6 +833,7 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
832833
} else if (node.isSetter) {
833834
var element = PropertyAccessorElementImpl(name, nameOffset);
834835
element.isAbstract = node.isAbstract;
836+
element.isAugmentation = node.augmentKeyword != null;
835837
element.isSetter = true;
836838
element.isStatic = node.isStatic;
837839

@@ -1244,6 +1246,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
12441246
required String name,
12451247
required PropertyAccessorElementImpl accessorElement,
12461248
}) {
1249+
if (accessorElement.isAugmentation) {
1250+
return;
1251+
}
1252+
12471253
var enclosingRef = _enclosingContext.reference;
12481254
var enclosingElement = _enclosingContext.element;
12491255

0 commit comments

Comments
 (0)