Skip to content

Commit 9e1ac08

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add 'ElementImpl2.reference', implement for ClassElement2.
Change-Id: Ie555b0032bba93f9c3d0dcd4cdef819eec6e1187 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391841 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 5eaefe7 commit 9e1ac08

File tree

117 files changed

+4310
-2766
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+4310
-2766
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import 'package:meta/meta.dart';
9696
// TODO(scheglov): Clean up the list of implicitly analyzed files.
9797
class AnalysisDriver {
9898
/// The version of data format, should be incremented on every format change.
99-
static const int DATA_VERSION = 402;
99+
static const int DATA_VERSION = 403;
100100

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

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,16 @@ mixin AugmentableElement<T extends ElementImpl> on ElementImpl {
108108

109109
class AugmentedClassElementImpl extends AugmentedInterfaceElementImpl
110110
with MaybeAugmentedClassElementMixin {
111+
@override
112+
final Reference reference;
113+
111114
@override
112115
final ClassElementImpl declaration;
113116

114-
AugmentedClassElementImpl(this.declaration);
117+
AugmentedClassElementImpl(this.reference, this.declaration) {
118+
reference.element2 = this;
119+
declaration.augmentedInternal = this;
120+
}
115121
}
116122

117123
class AugmentedEnumElementImpl extends AugmentedInterfaceElementImpl
@@ -209,8 +215,7 @@ class BindPatternVariableElementImpl2 extends PatternVariableElementImpl2
209215
class ClassElementImpl extends ClassOrMixinElementImpl
210216
with AugmentableElement<ClassElementImpl>
211217
implements ClassElement, ClassFragment {
212-
late MaybeAugmentedClassElementMixin augmentedInternal =
213-
NotAugmentedClassElementImpl(this);
218+
late MaybeAugmentedClassElementMixin augmentedInternal;
214219

215220
/// Initialize a newly created class element to have the given [name] at the
216221
/// given [offset] in the file that contains the declaration of this element.
@@ -275,12 +280,6 @@ class ClassElementImpl extends ClassOrMixinElementImpl
275280

276281
@override
277282
MaybeAugmentedClassElementMixin get augmented {
278-
if (isAugmentation) {
279-
if (augmentationTarget case var augmentationTarget?) {
280-
return augmentationTarget.augmented;
281-
}
282-
}
283-
284283
linkedData?.read(this);
285284
return augmentedInternal;
286285
}
@@ -306,7 +305,9 @@ class ClassElementImpl extends ClassOrMixinElementImpl
306305
}
307306

308307
@override
309-
ClassElement2 get element => super.element as ClassElement2;
308+
MaybeAugmentedClassElementMixin get element {
309+
return augmented;
310+
}
310311

311312
@override
312313
set fields(List<FieldElementImpl> fields) {
@@ -3176,6 +3177,13 @@ abstract class ElementImpl2 implements Element2 {
31763177
@override
31773178
Element2 get nonSynthetic2 => this;
31783179

3180+
/// The reference of this element, used during reading summaries.
3181+
///
3182+
/// Can be `null` if this element cannot be referenced from outside,
3183+
/// for example a [LocalFunctionElement], a [TypeParameterElement],
3184+
/// a positional [FormalParameterElement], etc.
3185+
Reference? get reference => null;
3186+
31793187
@override
31803188
AnalysisSession? get session {
31813189
return enclosingElement2?.session;
@@ -6680,6 +6688,9 @@ mixin MaybeAugmentedClassElementMixin on MaybeAugmentedInterfaceElementMixin
66806688
@override
66816689
bool get isValidMixin => declaration.isValidMixin;
66826690

6691+
/// See [ElementImpl2.reference].
6692+
Reference get reference;
6693+
66836694
@override
66846695
T? accept2<T>(ElementVisitor2<T> visitor) {
66856696
return visitor.visitClassElement(this);
@@ -8295,17 +8306,23 @@ abstract class NonParameterVariableElementImpl extends VariableElementImpl
82958306

82968307
class NotAugmentedClassElementImpl extends NotAugmentedInterfaceElementImpl
82978308
with MaybeAugmentedClassElementMixin {
8309+
@override
8310+
final Reference reference;
8311+
82988312
@override
82998313
final ClassElementImpl element;
83008314

8301-
NotAugmentedClassElementImpl(this.element);
8315+
NotAugmentedClassElementImpl(this.reference, this.element) {
8316+
reference.element2 = this;
8317+
element.augmentedInternal = this;
8318+
}
83028319

83038320
@override
83048321
ClassElementImpl get declaration => element;
83058322

83068323
@override
83078324
AugmentedClassElementImpl toAugmented() {
8308-
var augmented = AugmentedClassElementImpl(declaration);
8325+
var augmented = AugmentedClassElementImpl(reference, declaration);
83098326
declaration.augmentedInternal = augmented;
83108327
return augmented;
83118328
}
@@ -8889,6 +8906,7 @@ class PrefixElementImpl extends _ExistingElementImpl implements PrefixElement {
88898906
}
88908907

88918908
class PrefixElementImpl2 extends ElementImpl2 implements PrefixElement2 {
8909+
@override
88928910
final Reference reference;
88938911

88948912
@override

pkg/analyzer/lib/src/generated/testing/element_factory.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analyzer/src/dart/element/type.dart';
1515
import 'package:analyzer/src/generated/engine.dart';
1616
import 'package:analyzer/src/generated/source.dart' show NonExistingSource;
1717
import 'package:analyzer/src/generated/utilities_dart.dart';
18+
import 'package:analyzer/src/summary2/reference.dart';
1819
import 'package:collection/collection.dart';
1920
import 'package:meta/meta.dart';
2021
import 'package:path/path.dart';
@@ -44,6 +45,7 @@ class ElementFactory {
4445
String typeName, InterfaceType? superclassType,
4546
[List<String>? parameterNames]) {
4647
ClassElementImpl element = ClassElementImpl(typeName, 0);
48+
NotAugmentedClassElementImpl(Reference.root(), element);
4749
element.constructors = const <ConstructorElementImpl>[];
4850
element.supertype = superclassType;
4951
if (parameterNames != null) {
@@ -68,6 +70,7 @@ class ElementFactory {
6870
supertype ??= objectType;
6971

7072
var element = ClassElementImpl(name, 0);
73+
NotAugmentedClassElementImpl(Reference.root(), element);
7174
element.typeParameters = typeParameters;
7275
element.supertype = supertype;
7376
element.mixins = mixins;

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,28 @@ import 'package:analyzer/src/utilities/extensions/string.dart';
1313

1414
class AugmentedClassDeclarationBuilder
1515
extends AugmentedInstanceDeclarationBuilder {
16-
final ClassElementImpl declaration;
16+
final ClassElementImpl firstFragment;
1717

1818
AugmentedClassDeclarationBuilder({
19-
required this.declaration,
19+
required MaybeAugmentedClassElementMixin element,
20+
required this.firstFragment,
2021
}) {
21-
addFields(declaration.fields);
22-
addConstructors(declaration.constructors);
23-
addAccessors(declaration.accessors);
24-
addMethods(declaration.methods);
22+
firstFragment.augmentedInternal = element;
23+
addFields(firstFragment.fields);
24+
addConstructors(firstFragment.constructors);
25+
addAccessors(firstFragment.accessors);
26+
addMethods(firstFragment.methods);
2527
}
2628

27-
void augment(ClassElementImpl element) {
28-
addFields(element.fields);
29-
addConstructors(element.constructors);
30-
addAccessors(element.accessors);
31-
addMethods(element.methods);
32-
_updatedAugmented(element);
29+
void augment(ClassElementImpl fragment) {
30+
var element = _ensureAugmented(firstFragment);
31+
fragment.augmentedInternal = firstFragment.augmentedInternal;
32+
33+
addFields(fragment.fields);
34+
addConstructors(fragment.constructors);
35+
addAccessors(fragment.accessors);
36+
addMethods(fragment.methods);
37+
_updatedAugmented(element, fragment);
3338
}
3439
}
3540

@@ -47,11 +52,14 @@ class AugmentedEnumDeclarationBuilder
4752
}
4853

4954
void augment(EnumElementImpl element) {
55+
var augmented = _ensureAugmented(declaration);
56+
element.augmentedInternal = declaration.augmentedInternal;
57+
5058
addFields(element.fields);
5159
addConstructors(element.constructors);
5260
addAccessors(element.accessors);
5361
addMethods(element.methods);
54-
_updatedAugmented(element);
62+
_updatedAugmented(augmented, element);
5563
}
5664
}
5765

@@ -68,10 +76,13 @@ class AugmentedExtensionDeclarationBuilder
6876
}
6977

7078
void augment(ExtensionElementImpl element) {
79+
var augmented = _ensureAugmented(declaration);
80+
element.augmentedInternal = declaration.augmentedInternal;
81+
7182
addFields(element.fields);
7283
addAccessors(element.accessors);
7384
addMethods(element.methods);
74-
_updatedAugmented(element);
85+
_updatedAugmented(augmented, element);
7586
}
7687
}
7788

@@ -89,11 +100,14 @@ class AugmentedExtensionTypeDeclarationBuilder
89100
}
90101

91102
void augment(ExtensionTypeElementImpl element) {
103+
var augmented = _ensureAugmented(declaration);
104+
element.augmentedInternal = declaration.augmentedInternal;
105+
92106
addFields(element.fields);
93107
addConstructors(element.constructors);
94108
addAccessors(element.accessors);
95109
addMethods(element.methods);
96-
_updatedAugmented(element);
110+
_updatedAugmented(augmented, element);
97111
}
98112
}
99113

@@ -183,7 +197,7 @@ abstract class AugmentedInstanceDeclarationBuilder {
183197
}
184198
}
185199

186-
AugmentedInstanceElementImpl? _ensureAugmented(
200+
AugmentedInstanceElementImpl _ensureAugmented(
187201
InstanceElementImpl augmentation,
188202
) {
189203
var maybeAugmented = augmentation.augmented;
@@ -229,13 +243,10 @@ abstract class AugmentedInstanceDeclarationBuilder {
229243
return target;
230244
}
231245

232-
void _updatedAugmented(InstanceElementImpl augmentation) {
233-
assert(augmentation.augmentationTarget != null);
234-
var augmented = _ensureAugmented(augmentation);
235-
if (augmented == null) {
236-
return;
237-
}
238-
246+
void _updatedAugmented(
247+
AugmentedInstanceElementImpl augmented,
248+
InstanceElementImpl augmentation,
249+
) {
239250
var declaration = augmented.declaration;
240251
var declarationTypeParameters = declaration.typeParameters;
241252

@@ -320,10 +331,13 @@ class AugmentedMixinDeclarationBuilder
320331
}
321332

322333
void augment(MixinElementImpl element) {
334+
var augmented = _ensureAugmented(declaration);
335+
element.augmentedInternal = declaration.augmentedInternal;
336+
323337
addFields(element.fields);
324338
addAccessors(element.accessors);
325339
addMethods(element.methods);
326-
_updatedAugmented(element);
340+
_updatedAugmented(augmented, element);
327341
}
328342
}
329343

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class ClassElementLinkedData extends ElementLinkedData<ClassElementImpl> {
154154

155155
if (element.augmentationTarget == null) {
156156
if (reader.readBool()) {
157-
var augmented = AugmentedClassElementImpl(element);
157+
var augmented = element.augmentedInternal as AugmentedClassElementImpl;
158158
element.augmentedInternal = augmented;
159159
augmented.mixins = reader._readInterfaceTypeList();
160160
augmented.interfaces = reader._readInterfaceTypeList();
@@ -754,12 +754,24 @@ class LibraryReader {
754754
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
755755

756756
var reference = _readReference();
757+
758+
var reference2 = _readReference();
759+
var isAugmented = _reader.readBool();
760+
757761
var fragmentName = _readFragmentName();
758762
var name = fragmentName?.name ?? '';
759763

760764
var element = ClassElementImpl(name, -1);
761765
element.name2 = fragmentName;
762766

767+
if (reference2.element2 case MaybeAugmentedClassElementMixin element2?) {
768+
element.augmentedInternal = element2;
769+
} else if (isAugmented) {
770+
AugmentedClassElementImpl(reference2, element);
771+
} else {
772+
NotAugmentedClassElementImpl(reference2, element);
773+
}
774+
763775
var linkedData = ClassElementLinkedData(
764776
reference: reference,
765777
libraryReader: this,
@@ -1809,13 +1821,6 @@ class LibraryReader {
18091821
);
18101822
});
18111823

1812-
unitElement.parts = _reader.readTypedList(() {
1813-
return _readPartElement(
1814-
libraryElement: libraryElement,
1815-
containerUnit: unitElement,
1816-
);
1817-
});
1818-
18191824
_readClasses(unitElement, unitReference);
18201825
_readEnums(unitElement, unitReference);
18211826
_readExtensions(unitElement, unitReference);
@@ -1839,6 +1844,13 @@ class LibraryReader {
18391844
);
18401845
});
18411846

1847+
unitElement.parts = _reader.readTypedList(() {
1848+
return _readPartElement(
1849+
libraryElement: libraryElement,
1850+
containerUnit: unitElement,
1851+
);
1852+
});
1853+
18421854
return unitElement;
18431855
}
18441856

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ class BundleWriter {
156156
_sink.writeUInt30(_resolutionSink.offset);
157157

158158
_writeReference(element);
159+
_writeReference2(element.augmented.reference);
160+
_sink.writeBool(element.augmentedInternal is AugmentedClassElementImpl);
159161
_writeFragmentName(element.name2);
160162
ClassElementFlags.write(_sink, element);
161163

@@ -755,8 +757,9 @@ class BundleWriter {
755757
_writeList(unitElement.libraryImports, _writeImportElement);
756758
_writeList(unitElement.libraryExports, _writeExportElement);
757759

760+
// Write the metadata for parts here, even though we write parts below.
761+
// The reason is that resolution data must be in a single chunk.
758762
_writePartElementsMetadata(unitElement);
759-
_writeList(unitElement.parts, _writePartElement);
760763

761764
_writeList(unitElement.classes, _writeClassElement);
762765
_writeList(unitElement.enums, _writeEnumElement);
@@ -781,6 +784,10 @@ class BundleWriter {
781784
_sink.writeStringUtf8(macroGenerated.code);
782785
_sink.writeUint8List(macroGenerated.informativeBytes);
783786
});
787+
788+
// Write parts after this library fragment, so that when we read, we
789+
// process fragments of declarations in the same order as we build them.
790+
_writeList(unitElement.parts, _writePartElement);
784791
}
785792

786793
static TypeParameterVarianceTag _encodeVariance(

0 commit comments

Comments
 (0)