Skip to content

Commit c69d325

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate AstDataExtractor and _InheritanceDataComputer.
Also add `Element2.lookupName`, because of `unary-`. Change-Id: If69cf9b512ccf3f8274cb18106a1fa50557c7afa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396480 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 4b39eef commit c69d325

File tree

9 files changed

+255
-48
lines changed

9 files changed

+255
-48
lines changed

pkg/analyzer/analyzer_use_new_elements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ lib/src/task/strong_mode.dart
162162
lib/src/test_utilities/find_element.dart
163163
lib/src/test_utilities/find_node.dart
164164
lib/src/test_utilities/mock_sdk_elements.dart
165-
lib/src/util/ast_data_extractor.dart
166165
lib/src/utilities/extensions/analysis_session.dart
167166
lib/src/utilities/extensions/ast.dart
168167
lib/src/utilities/extensions/element.dart
@@ -192,7 +191,6 @@ test/id_tests/definite_unassignment_test.dart
192191
test/id_tests/exhaustiveness_test.dart
193192
test/id_tests/inferred_type_arguments_test.dart
194193
test/id_tests/inferred_variable_types_test.dart
195-
test/id_tests/inheritance_test.dart
196194
test/id_tests/metadata_evaluate_test.dart
197195
test/id_tests/metadata_test.dart
198196
test/id_tests/nullability_test.dart

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,24 @@ abstract class Element2 {
441441
/// The object can be used to locate this element at a later time.
442442
ElementLocation? get location;
443443

444+
/// The name to use for lookup in maps.
445+
///
446+
/// It is usually the same as [name3], with a few special cases.
447+
///
448+
/// Just like [name3], it can be `null` if the element does not have
449+
/// a name, for example an unnamed extension, or because of parser recovery.
450+
///
451+
/// For a [SetterElement] the result has `=` at the end.
452+
///
453+
/// For an unary operator `-` the result is `unary-`.
454+
/// For a binary operator `-` the result is just `-`.
455+
String? get lookupName;
456+
444457
/// The name of this element.
445458
///
446459
/// Returns `null` if this element doesn't have a name.
460+
///
461+
/// See [Fragment.name2] for details.
447462
String? get name3;
448463

449464
/// The non-synthetic element that caused this element to be created.
@@ -978,6 +993,8 @@ abstract class Fragment {
978993
/// for such synthetic fragments.
979994
///
980995
/// For a [SetterFragment] this is the identifier, without `=` at the end.
996+
///
997+
/// For both unary and binary `-` operator this is `-`.
981998
String? get name2;
982999

9831000
/// The offset of the [name2] of this element.

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2924,6 +2924,11 @@ abstract class ElementImpl implements Element, Element2 {
29242924
return _cachedLocation ??= ElementLocationImpl.con1(this);
29252925
}
29262926

2927+
@override
2928+
String? get lookupName {
2929+
return name3;
2930+
}
2931+
29272932
@override
29282933
List<ElementAnnotationImpl> get metadata {
29292934
return _metadata;
@@ -3279,6 +3284,11 @@ abstract class ElementImpl2 implements Element2 {
32793284
return _cachedLocation ??= ElementLocationImpl.fromElement(this);
32803285
}
32813286

3287+
@override
3288+
String? get lookupName {
3289+
return name3;
3290+
}
3291+
32823292
@override
32833293
Element2 get nonSynthetic2 => this;
32843294

@@ -7782,6 +7792,14 @@ class MethodElementImpl2 extends ExecutableElementImpl2
77827792
@override
77837793
ElementKind get kind => ElementKind.METHOD;
77847794

7795+
@override
7796+
String? get lookupName {
7797+
if (name3 == '-' && formalParameters.isEmpty) {
7798+
return 'unary-';
7799+
}
7800+
return name3;
7801+
}
7802+
77857803
@override
77867804
T? accept2<T>(ElementVisitor2<T> visitor) {
77877805
return visitor.visitMethodElement(this);
@@ -9756,6 +9774,14 @@ class SetterElementImpl extends PropertyAccessorElementImpl2
97569774
@override
97579775
ElementKind get kind => ElementKind.SETTER;
97589776

9777+
@override
9778+
String? get lookupName {
9779+
if (name3 case var name?) {
9780+
return '$name=';
9781+
}
9782+
return null;
9783+
}
9784+
97599785
@override
97609786
Element2 get nonSynthetic2 {
97619787
if (!isSynthetic) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class ConstructorMember extends ExecutableMember
6868
@override
6969
bool get isFactory => declaration.isFactory;
7070

71+
@override
72+
String? get lookupName => _element2.lookupName;
73+
7174
@override
7275
String get name => declaration.name;
7376

@@ -557,6 +560,9 @@ class FieldMember extends VariableMember
557560
@override
558561
LibraryElement2? get library2 => _element2.library2;
559562

563+
@override
564+
String? get lookupName => _element2.lookupName;
565+
560566
@override
561567
String get name => declaration.name;
562568

@@ -694,6 +700,9 @@ class GetterMember extends PropertyAccessorMember implements GetterElement {
694700
@override
695701
GetterFragment get firstFragment => _element2.firstFragment;
696702

703+
@override
704+
String? get lookupName => _element2.lookupName;
705+
697706
@override
698707
PropertyInducingElement2? get variable3 =>
699708
variable2.asElement2 as PropertyInducingElement2?;
@@ -990,6 +999,9 @@ class MethodMember extends ExecutableMember
990999
@override
9911000
MethodFragment get firstFragment => _element2.firstFragment;
9921001

1002+
@override
1003+
String? get lookupName => name3;
1004+
9931005
@override
9941006
String get name => declaration.name;
9951007

@@ -1130,6 +1142,9 @@ class ParameterMember extends VariableMember
11301142
@override
11311143
LibraryElement2? get library2 => _element2.library2;
11321144

1145+
@override
1146+
String? get lookupName => _element2.lookupName;
1147+
11331148
@override
11341149
String get name => declaration.name;
11351150

@@ -1397,6 +1412,9 @@ class SetterMember extends PropertyAccessorMember implements SetterElement {
13971412
@override
13981413
SetterFragment get firstFragment => _element2.firstFragment;
13991414

1415+
@override
1416+
String? get lookupName => _element2.lookupName;
1417+
14001418
@override
14011419
PropertyInducingElement2? get variable3 =>
14021420
variable2.asElement2 as PropertyInducingElement2?;

pkg/analyzer/lib/src/generated/resolver.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,9 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
718718
if (flowAnalysis.dataForTesting != null) {
719719
var nonPromotionReasonText = entry.value.shortName;
720720
var args = <String>[];
721-
if (whyNotPromotedVisitor.propertyReference != null) {
722-
var id =
723-
computeMemberId(whyNotPromotedVisitor.propertyReference!);
721+
var propertyReference = whyNotPromotedVisitor.propertyReference;
722+
if (propertyReference != null) {
723+
var id = computeMemberId(propertyReference.asElement2);
724724
args.add('target: $id');
725725
}
726726
var propertyType = whyNotPromotedVisitor.propertyType;

pkg/analyzer/lib/src/util/ast_data_extractor.dart

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@
55
import 'package:_fe_analyzer_shared/src/testing/id.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99

10-
MemberId computeMemberId(Element element) {
11-
var enclosingElement = element.enclosingElement3;
12-
if (enclosingElement is CompilationUnitElement) {
13-
var memberName = element.name!;
14-
if (element is PropertyAccessorElement && element.isSetter) {
10+
MemberId computeMemberId(Element2 element) {
11+
var enclosingElement = element.enclosingElement2;
12+
if (enclosingElement is LibraryElement2) {
13+
var memberName = element.name3!;
14+
if (element is SetterElement) {
1515
memberName += '=';
1616
}
1717
return MemberId.internal(memberName);
18-
} else if (enclosingElement is InterfaceElement) {
19-
var memberName = element.name!;
20-
var className = enclosingElement.name;
18+
} else if (enclosingElement is InterfaceElement2) {
19+
var memberName = element.lookupName!;
20+
var className = enclosingElement.name3;
2121
return MemberId.internal(memberName, className: className);
22-
} else if (enclosingElement is ExtensionElement) {
23-
var memberName = element.name!;
24-
var extensionName = enclosingElement.name;
25-
if (element is PropertyAccessorElement) {
26-
memberName = '${element.isGetter ? 'get' : 'set'}#$memberName';
22+
} else if (enclosingElement is ExtensionElement2) {
23+
var memberName = element.name3!;
24+
var extensionName = enclosingElement.name3;
25+
if (element is PropertyAccessorElement2) {
26+
if (element is GetterElement) {
27+
memberName = 'get#$memberName';
28+
} else {
29+
memberName = 'set#$memberName';
30+
}
2731
}
2832
return MemberId.internal('$extensionName|$memberName');
2933
}
@@ -45,7 +49,7 @@ abstract class AstDataExtractor<T> extends GeneralizingAstVisitor<void>
4549
NodeId computeDefaultNodeId(AstNode node) =>
4650
NodeId(_nodeOffset(node), IdKind.node);
4751

48-
T? computeElementValue(Id id, Element element) => null;
52+
T? computeElementValue(Id id, Element2 element) => null;
4953

5054
void computeForClass(Declaration node, Id? id) {
5155
if (id == null) return;
@@ -65,7 +69,7 @@ abstract class AstDataExtractor<T> extends GeneralizingAstVisitor<void>
6569
registerValue(uri, _nodeOffset(node), id, value, node);
6670
}
6771

68-
void computeForLibrary(LibraryElement library, Id? id) {
72+
void computeForLibrary(LibraryElement2 library, Id? id) {
6973
if (id == null) return;
7074
T? value = computeElementValue(id, library);
7175
registerValue(uri, 0, id, value, library);
@@ -101,12 +105,12 @@ abstract class AstDataExtractor<T> extends GeneralizingAstVisitor<void>
101105
T? computeNodeValue(Id id, AstNode node);
102106

103107
Id createClassId(Declaration node) {
104-
var element = node.declaredElement!;
105-
return ClassId(element.name!);
108+
var element = node.declaredFragment!.element;
109+
return ClassId(element.name3!);
106110
}
107111

108-
Id createLibraryId(LibraryElement node) {
109-
Uri uri = node.source.uri;
112+
Id createLibraryId(LibraryElement2 node) {
113+
Uri uri = node.uri;
110114
if (uri.path.startsWith(r'/C:')) {
111115
// The `MemoryResourceProvider.convertPath` inserts '/C:' on Windows.
112116
uri = Uri(scheme: uri.scheme, path: uri.path.substring(3));
@@ -115,7 +119,7 @@ abstract class AstDataExtractor<T> extends GeneralizingAstVisitor<void>
115119
}
116120

117121
Id createMemberId(Declaration node) {
118-
var element = node.declaredElement!;
122+
var element = node.declaredFragment!.element;
119123
return computeMemberId(element);
120124
}
121125

@@ -151,7 +155,7 @@ abstract class AstDataExtractor<T> extends GeneralizingAstVisitor<void>
151155

152156
@override
153157
void visitCompilationUnit(CompilationUnit node) {
154-
var library = node.declaredElement!.library;
158+
var library = node.declaredFragment!.element;
155159
computeForLibrary(library, createLibraryId(library));
156160
super.visitCompilationUnit(node);
157161
}

pkg/analyzer/test/id_tests/inheritance_test.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'dart:io';
77
import 'package:_fe_analyzer_shared/src/testing/id.dart';
88
import 'package:_fe_analyzer_shared/src/testing/id_testing.dart';
99
import 'package:analyzer/dart/ast/ast.dart';
10-
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer/dart/element/type.dart';
1212
import 'package:analyzer/error/error.dart';
1313
import 'package:analyzer/src/dart/analysis/testing_data.dart';
@@ -31,7 +31,7 @@ main(List<String> args) async {
3131

3232
String supertypeToString(InterfaceType type) {
3333
var sb = StringBuffer();
34-
sb.write(type.element.name);
34+
sb.write(type.element3.name3);
3535
if (type.typeArguments.isNotEmpty) {
3636
sb.write('<');
3737
var comma = '';
@@ -63,7 +63,7 @@ class _InheritanceDataComputer extends DataComputer<String> {
6363
@override
6464
void computeUnitData(TestingData testingData, CompilationUnit unit,
6565
Map<Id, ActualData<String>> actualMap) {
66-
_InheritanceDataExtractor(unit.declaredElement!.source.uri, actualMap)
66+
_InheritanceDataExtractor(unit.declaredFragment!.source.uri, actualMap)
6767
.run(unit);
6868
}
6969
}
@@ -74,48 +74,48 @@ class _InheritanceDataExtractor extends AstDataExtractor<String> {
7474
_InheritanceDataExtractor(super.uri, super.actualMap);
7575

7676
@override
77-
String? computeElementValue(Id id, Element element) {
77+
String? computeElementValue(Id id, Element2 element) {
7878
return null;
7979
}
8080

8181
@override
8282
void computeForClass(Declaration node, Id? id) {
8383
super.computeForClass(node, id);
8484
if (node is ClassDeclaration) {
85-
var element = node.declaredElement!;
85+
var element = node.declaredFragment!.element;
8686

8787
void registerMember(
8888
MemberId id, int offset, Object object, DartType type) {
8989
registerValue(uri, offset, id, type.getDisplayString(), object);
9090
}
9191

92-
var interface = inheritance.getInterface(element);
93-
for (var name in interface.map.keys) {
94-
var executable = interface.map[name]!;
92+
var interface = inheritance.getInterface2(element);
93+
for (var name in interface.map2.keys) {
94+
var executable = interface.map2[name]!;
9595

96-
var enclosingClass = executable.enclosingElement3 as InterfaceElement;
97-
if (enclosingClass is ClassElement && enclosingClass.isDartCoreObject) {
96+
var enclosingClass = executable.enclosingElement2 as InterfaceElement2;
97+
if (enclosingClass is ClassElement2 &&
98+
enclosingClass.isDartCoreObject) {
9899
continue;
99100
}
100101

101102
var id = MemberId.internal(
102103
name.name,
103-
className: element.name,
104+
className: element.name3,
104105
);
105106

106107
var offset = enclosingClass == element
107-
? executable.nameOffset
108-
: element.nameOffset;
108+
? executable.firstFragment.nameOffset2
109+
: element.firstFragment.nameOffset2;
110+
offset ??= -1;
109111

110112
DartType type;
111-
if (executable is MethodElement) {
113+
if (executable is MethodElement2) {
112114
type = executable.type;
113-
} else if (executable is PropertyAccessorElement) {
114-
if (executable.isGetter) {
115-
type = executable.returnType;
116-
} else {
117-
type = executable.parameters.first.type;
118-
}
115+
} else if (executable is GetterElement) {
116+
type = executable.returnType;
117+
} else if (executable is SetterElement) {
118+
type = executable.formalParameters.first.type;
119119
} else {
120120
throw UnimplementedError('(${executable.runtimeType}) $executable');
121121
}
@@ -128,7 +128,7 @@ class _InheritanceDataExtractor extends AstDataExtractor<String> {
128128
@override
129129
String? computeNodeValue(Id id, AstNode node) {
130130
if (node is ClassDeclaration) {
131-
var cls = node.declaredElement!;
131+
var cls = node.declaredFragment!.element;
132132
var supertypes = <String>[];
133133
supertypes.add(supertypeToString(cls.thisType));
134134
for (var supertype in cls.allSupertypes) {

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,23 @@ class _Element2Writer extends _AbstractElementWriter {
449449
void _writeElementName(Element2 e) {
450450
var name = e.name3 ?? '<null-name>';
451451
_sink.write(name);
452+
453+
switch (e) {
454+
case MethodElement2():
455+
if (e.name3 == '-' && e.formalParameters.isEmpty) {
456+
expect(e.lookupName, 'unary-');
457+
} else {
458+
expect(e.lookupName, e.name3);
459+
}
460+
case SetterElement():
461+
if (e.name3 case var name?) {
462+
expect(e.lookupName, '$name=');
463+
} else {
464+
expect(e.lookupName, isNull);
465+
}
466+
default:
467+
expect(e.lookupName, e.name3);
468+
}
452469
}
453470

454471
void _writeElementReference(String name, Element2? e) {

0 commit comments

Comments
 (0)