Skip to content

Commit 19c78e2

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate ElementWalker.
Change-Id: Ide09d0ee50c5650ec4bd2ebc6937bac5dab66cc6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411880 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 1e148e6 commit 19c78e2

File tree

3 files changed

+104
-148
lines changed

3 files changed

+104
-148
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5480,8 +5480,8 @@ class GenericFunctionTypeElementImpl extends _ExistingElementImpl
54805480
/// Set the parameters defined by this function type element to the given
54815481
/// [parameters].
54825482
set parameters(List<ParameterElementImpl> parameters) {
5483-
for (ParameterElement parameter in parameters) {
5484-
(parameter as ParameterElementImpl).enclosingElement3 = this;
5483+
for (var parameter in parameters) {
5484+
parameter.enclosingElement3 = this;
54855485
}
54865486
_parameters = parameters;
54875487
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
12011201
void visitRepresentationDeclaration(
12021202
covariant RepresentationDeclarationImpl node,
12031203
) {
1204-
var element = _elementWalker!.element as ExtensionTypeElementImpl;
1204+
var element = _elementWalker!.fragment as ExtensionTypeElementImpl;
12051205
if (element.augmentationTarget == null) {
12061206
node.fieldElement = _elementWalker!.getVariable() as FieldElementImpl;
12071207
node.constructorElement = _elementWalker!.getConstructor();

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

Lines changed: 101 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -2,132 +2,131 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore_for_file: analyzer_use_new_elements
6-
7-
import 'package:analyzer/dart/element/element.dart';
85
import 'package:analyzer/src/dart/element/element.dart';
96

10-
/// Keeps track of the set of non-synthetic child elements of an element,
7+
/// Keeps track of the set of non-synthetic child fragments of a fragment,
118
/// yielding them one at a time in response to "get" method calls.
129
class ElementWalker {
13-
/// The element whose child elements are being walked.
14-
final Element element;
10+
/// The fragment whose child fragments are being walked.
11+
final ElementImpl fragment;
1512
String? libraryFilePath;
1613
String? unitFilePath;
1714

18-
List<PropertyAccessorElement>? _accessors;
15+
List<PropertyAccessorElementImpl>? _accessors;
1916
int _accessorIndex = 0;
20-
List<ClassElement>? _classes;
17+
List<ClassElementImpl>? _classes;
2118
int _classIndex = 0;
22-
List<ConstructorElement>? _constructors;
19+
List<ConstructorElementImpl>? _constructors;
2320
int _constructorIndex = 0;
24-
List<EnumElement>? _enums;
21+
List<EnumElementImpl>? _enums;
2522
int _enumIndex = 0;
26-
List<ExtensionElement>? _extensions;
23+
List<ExtensionElementImpl>? _extensions;
2724
int _extensionIndex = 0;
28-
List<ExtensionTypeElement>? _extensionTypes;
25+
List<ExtensionTypeElementImpl>? _extensionTypes;
2926
int _extensionTypeIndex = 0;
30-
List<ExecutableElement>? _functions;
27+
List<ExecutableElementImpl>? _functions;
3128
int _functionIndex = 0;
32-
List<MixinElement>? _mixins;
29+
List<MixinElementImpl>? _mixins;
3330
int _mixinIndex = 0;
34-
List<ParameterElement>? _parameters;
31+
List<ParameterElementImpl>? _parameters;
3532
int _parameterIndex = 0;
36-
List<TypeAliasElement>? _typedefs;
33+
List<TypeAliasElementImpl>? _typedefs;
3734
int _typedefIndex = 0;
38-
List<TypeParameterElement>? _typeParameters;
35+
List<TypeParameterElementImpl>? _typeParameters;
3936
int _typeParameterIndex = 0;
40-
List<VariableElement>? _variables;
37+
List<VariableElementImpl>? _variables;
4138
int _variableIndex = 0;
4239

4340
/// Creates an [ElementWalker] which walks the child elements of a class
4441
/// element.
45-
ElementWalker.forClass(ClassElement this.element)
46-
: _accessors = element.accessors.where(_isNotSynthetic).toList(),
47-
_constructors = element.isMixinApplication
42+
ElementWalker.forClass(ClassElementImpl this.fragment)
43+
: _accessors = fragment.accessors.where(_isNotSynthetic).toList(),
44+
_constructors = fragment.isMixinApplication
4845
? null
49-
: element.constructors.where(_isNotSynthetic).toList(),
50-
_functions = element.methods,
51-
_typeParameters = element.typeParameters,
52-
_variables = element.fields.where(_isNotSynthetic).toList();
46+
: fragment.constructors.where(_isNotSynthetic).toList(),
47+
_functions = fragment.methods,
48+
_typeParameters = fragment.typeParameters,
49+
_variables = fragment.fields.where(_isNotSynthetic).toList();
5350

5451
/// Creates an [ElementWalker] which walks the child elements of a compilation
5552
/// unit element.
56-
ElementWalker.forCompilationUnit(CompilationUnitElementImpl this.element,
53+
ElementWalker.forCompilationUnit(CompilationUnitElementImpl this.fragment,
5754
{this.libraryFilePath, this.unitFilePath})
58-
: _accessors = element.accessors.where(_isNotSynthetic).toList(),
59-
_classes = element.classes,
60-
_enums = element.enums,
61-
_extensions = element.extensions,
62-
_extensionTypes = element.extensionTypes,
63-
_functions = element.functions,
64-
_mixins = element.mixins,
65-
_typedefs = element.typeAliases,
66-
_variables = element.topLevelVariables.where(_isNotSynthetic).toList();
55+
: _accessors = fragment.accessors.where(_isNotSynthetic).toList(),
56+
_classes = fragment.classes,
57+
_enums = fragment.enums,
58+
_extensions = fragment.extensions,
59+
_extensionTypes = fragment.extensionTypes,
60+
_functions = fragment.functions,
61+
_mixins = fragment.mixins,
62+
_typedefs = fragment.typeAliases,
63+
_variables = fragment.topLevelVariables.where(_isNotSynthetic).toList();
6764

6865
/// Creates an [ElementWalker] which walks the child elements of a enum
6966
/// element.
70-
ElementWalker.forEnum(EnumElement this.element)
71-
: _accessors = element.accessors.where(_isNotSynthetic).toList(),
72-
_constructors = element.constructors.where(_isNotSynthetic).toList(),
73-
_functions = element.methods,
74-
_typeParameters = element.typeParameters,
75-
_variables = element.fields.where(_isNotSynthetic).toList();
67+
ElementWalker.forEnum(EnumElementImpl this.fragment)
68+
: _accessors = fragment.accessors.where(_isNotSynthetic).toList(),
69+
_constructors = fragment.constructors.where(_isNotSynthetic).toList(),
70+
_functions = fragment.methods,
71+
_typeParameters = fragment.typeParameters,
72+
_variables = fragment.fields.where(_isNotSynthetic).toList();
7673

7774
/// Creates an [ElementWalker] which walks the child elements of a compilation
7875
/// unit element.
79-
ElementWalker.forExecutable(ExecutableElement this.element)
80-
: _functions = const <ExecutableElement>[],
81-
_parameters = element.parameters,
82-
_typeParameters = element.typeParameters;
76+
ElementWalker.forExecutable(ExecutableElementImpl this.fragment)
77+
: _functions = const <ExecutableElementImpl>[],
78+
_parameters = fragment.parameters,
79+
_typeParameters = fragment.typeParameters;
8380

8481
/// Creates an [ElementWalker] which walks the child elements of an extension
8582
/// element.
86-
ElementWalker.forExtension(ExtensionElement this.element)
87-
: _accessors = element.accessors.where(_isNotSynthetic).toList(),
88-
_functions = element.methods,
89-
_typeParameters = element.typeParameters,
90-
_variables = element.fields.where(_isNotSynthetic).toList();
91-
92-
ElementWalker.forExtensionType(ExtensionTypeElementImpl this.element)
93-
: _accessors = element.accessors.where(_isNotSynthetic).toList(),
94-
_constructors = element.constructors,
95-
_functions = element.methods,
96-
_typeParameters = element.typeParameters,
97-
_variables = element.fields.where(_isNotSynthetic).toList();
83+
ElementWalker.forExtension(ExtensionElementImpl this.fragment)
84+
: _accessors = fragment.accessors.where(_isNotSynthetic).toList(),
85+
_functions = fragment.methods,
86+
_typeParameters = fragment.typeParameters,
87+
_variables = fragment.fields.where(_isNotSynthetic).toList();
88+
89+
ElementWalker.forExtensionType(ExtensionTypeElementImpl this.fragment)
90+
: _accessors = fragment.accessors.where(_isNotSynthetic).toList(),
91+
_constructors = fragment.constructors,
92+
_functions = fragment.methods,
93+
_typeParameters = fragment.typeParameters,
94+
_variables = fragment.fields.where(_isNotSynthetic).toList();
9895

9996
/// Creates an [ElementWalker] which walks the child elements of a typedef
10097
/// element.
101-
ElementWalker.forGenericFunctionType(GenericFunctionTypeElement this.element)
102-
: _parameters = element.parameters,
103-
_typeParameters = element.typeParameters;
98+
ElementWalker.forGenericFunctionType(
99+
GenericFunctionTypeElementImpl this.fragment)
100+
: _parameters = fragment.parameters,
101+
_typeParameters = fragment.typeParameters;
104102

105103
/// Creates an [ElementWalker] which walks the child elements of a typedef
106104
/// element defined using a generic function type.
107-
ElementWalker.forGenericTypeAlias(TypeAliasElement this.element)
108-
: _typeParameters = element.typeParameters;
105+
ElementWalker.forGenericTypeAlias(TypeAliasElementImpl this.fragment)
106+
: _typeParameters = fragment.typeParameters;
109107

110108
/// Creates an [ElementWalker] which walks the child elements of a mixin
111109
/// element.
112-
ElementWalker.forMixin(MixinElement this.element)
113-
: _accessors = element.accessors.where(_isNotSynthetic).toList(),
114-
_constructors = element.constructors.where(_isNotSynthetic).toList(),
115-
_functions = element.methods,
116-
_typeParameters = element.typeParameters,
117-
_variables = element.fields.where(_isNotSynthetic).toList();
110+
ElementWalker.forMixin(MixinElementImpl this.fragment)
111+
: _accessors = fragment.accessors.where(_isNotSynthetic).toList(),
112+
_constructors = fragment.constructors.where(_isNotSynthetic).toList(),
113+
_functions = fragment.methods,
114+
_typeParameters = fragment.typeParameters,
115+
_variables = fragment.fields.where(_isNotSynthetic).toList();
118116

119117
/// Creates an [ElementWalker] which walks the child elements of a parameter
120118
/// element.
121-
ElementWalker.forParameter(ParameterElement this.element)
122-
: _parameters = element.parameters,
123-
_typeParameters = element.typeParameters;
119+
ElementWalker.forParameter(ParameterElementImpl this.fragment)
120+
: _parameters = fragment.parameters,
121+
_typeParameters = fragment.typeParameters;
124122

125123
/// Creates an [ElementWalker] which walks the child elements of a typedef
126124
/// element.
127-
ElementWalker.forTypedef(TypeAliasElement this.element)
125+
ElementWalker.forTypedef(TypeAliasElementImpl this.fragment)
128126
: _parameters =
129-
(element.aliasedElement as GenericFunctionTypeElement).parameters,
130-
_typeParameters = element.typeParameters;
127+
(fragment.aliasedElement as GenericFunctionTypeElementImpl)
128+
.parameters,
129+
_typeParameters = fragment.typeParameters;
131130

132131
void consumeLocalElements() {
133132
_functionIndex = _functions!.length;
@@ -137,109 +136,66 @@ class ElementWalker {
137136
_parameterIndex = _parameters!.length;
138137
}
139138

140-
/// Returns the next non-synthetic child of [element] which is an accessor;
139+
/// Returns the next non-synthetic child of [fragment] which is an accessor;
141140
/// throws an [IndexError] if there are no more.
142141
PropertyAccessorElementImpl getAccessor() {
143-
// TODO(scheglov): Remove after fixing.
144-
// https://github.com/dart-lang/sdk/issues/46392
145-
var accessors = _accessors;
146-
if (accessors != null && _accessorIndex >= accessors.length) {
147-
throw StateError(
148-
'[_accessorIndex: $_accessorIndex]'
149-
'[_accessors.length: ${accessors.length}]'
150-
'[accessors: $accessors]'
151-
'[element.source: ${element.source?.fullName}]'
152-
'[libraryFilePath: $libraryFilePath]'
153-
'[unitFilePath: $unitFilePath]',
154-
);
155-
}
156-
return _accessors![_accessorIndex++] as PropertyAccessorElementImpl;
142+
return _accessors![_accessorIndex++];
157143
}
158144

159-
/// Returns the next non-synthetic child of [element] which is a class; throws
160-
/// an [IndexError] if there are no more.
145+
/// Returns the next non-synthetic child of [fragment] which is a class;
146+
/// throws an [IndexError] if there are no more.
161147
ClassElementImpl getClass() {
162-
// TODO(scheglov): Remove after fixing.
163-
// https://github.com/dart-lang/sdk/issues/46392
164-
var classes = _classes;
165-
if (classes != null && _classIndex >= classes.length) {
166-
throw StateError(
167-
'[_classIndex: $_classIndex]'
168-
'[classes.length: ${classes.length}]'
169-
'[classes: $classes]'
170-
'[element.source: ${element.source?.fullName}]'
171-
'[libraryFilePath: $libraryFilePath]'
172-
'[unitFilePath: $unitFilePath]',
173-
);
174-
}
175-
return _classes![_classIndex++] as ClassElementImpl;
148+
return _classes![_classIndex++];
176149
}
177150

178-
/// Returns the next non-synthetic child of [element] which is a constructor;
151+
/// Returns the next non-synthetic child of [fragment] which is a constructor;
179152
/// throws an [IndexError] if there are no more.
180153
ConstructorElementImpl getConstructor() =>
181-
_constructors![_constructorIndex++] as ConstructorElementImpl;
154+
_constructors![_constructorIndex++];
182155

183-
/// Returns the next non-synthetic child of [element] which is an enum; throws
184-
/// an [IndexError] if there are no more.
185-
EnumElementImpl getEnum() => _enums![_enumIndex++] as EnumElementImpl;
156+
/// Returns the next non-synthetic child of [fragment] which is an enum;
157+
/// throws an [IndexError] if there are no more.
158+
EnumElementImpl getEnum() => _enums![_enumIndex++];
186159

187-
ExtensionElementImpl getExtension() =>
188-
_extensions![_extensionIndex++] as ExtensionElementImpl;
160+
ExtensionElementImpl getExtension() => _extensions![_extensionIndex++];
189161

190162
ExtensionTypeElementImpl getExtensionType() =>
191-
_extensionTypes![_extensionTypeIndex++] as ExtensionTypeElementImpl;
163+
_extensionTypes![_extensionTypeIndex++];
192164

193-
/// Returns the next non-synthetic child of [element] which is a top level
165+
/// Returns the next non-synthetic child of [fragment] which is a top level
194166
/// function, method, or local function; throws an [IndexError] if there are
195167
/// no more.
196-
ExecutableElementImpl getFunction() =>
197-
_functions![_functionIndex++] as ExecutableElementImpl;
168+
ExecutableElementImpl getFunction() => _functions![_functionIndex++];
198169

199-
/// Returns the next non-synthetic child of [element] which is a mixin; throws
200-
/// an [IndexError] if there are no more.
201-
MixinElementImpl getMixin() => _mixins![_mixinIndex++] as MixinElementImpl;
170+
/// Returns the next non-synthetic child of [fragment] which is a mixin;
171+
/// throws an [IndexError] if there are no more.
172+
MixinElementImpl getMixin() => _mixins![_mixinIndex++];
202173

203-
/// Returns the next non-synthetic child of [element] which is a parameter;
174+
/// Returns the next non-synthetic child of [fragment] which is a parameter;
204175
/// throws an [IndexError] if there are no more.
205-
ParameterElementImpl getParameter() =>
206-
_parameters![_parameterIndex++] as ParameterElementImpl;
176+
ParameterElementImpl getParameter() => _parameters![_parameterIndex++];
207177

208-
/// Returns the next non-synthetic child of [element] which is a typedef;
178+
/// Returns the next non-synthetic child of [fragment] which is a typedef;
209179
/// throws an [IndexError] if there are no more.
210-
TypeAliasElementImpl getTypedef() =>
211-
_typedefs![_typedefIndex++] as TypeAliasElementImpl;
180+
TypeAliasElementImpl getTypedef() => _typedefs![_typedefIndex++];
212181

213-
/// Returns the next non-synthetic child of [element] which is a type
182+
/// Returns the next non-synthetic child of [fragment] which is a type
214183
/// parameter; throws an [IndexError] if there are no more.
215184
TypeParameterElementImpl getTypeParameter() =>
216-
_typeParameters![_typeParameterIndex++] as TypeParameterElementImpl;
185+
_typeParameters![_typeParameterIndex++];
217186

218-
/// Returns the next non-synthetic child of [element] which is a top level
187+
/// Returns the next non-synthetic child of [fragment] which is a top level
219188
/// variable, field, or local variable; throws an [IndexError] if there are no
220189
/// more.
221190
VariableElementImpl getVariable() {
222-
// TODO(scheglov): Remove after fixing.
223-
// https://github.com/dart-lang/sdk/issues/46392
224-
var variables = _variables;
225-
if (variables != null && _variableIndex >= variables.length) {
226-
throw StateError(
227-
'[_variableIndex: $_variableIndex]'
228-
'[_variables.length: ${variables.length}]'
229-
'[variables: $variables]'
230-
'[element.source: ${element.source?.fullName}]'
231-
'[libraryFilePath: $libraryFilePath]'
232-
'[unitFilePath: $unitFilePath]',
233-
);
234-
}
235-
return _variables![_variableIndex++] as VariableElementImpl;
191+
return _variables![_variableIndex++];
236192
}
237193

238-
/// Verifies that all non-synthetic children of [element] have been obtained
194+
/// Verifies that all non-synthetic children of [fragment] have been obtained
239195
/// from their corresponding "get" method calls; if not, throws a
240196
/// [StateError].
241197
void validate() {
242-
void check(List<Element>? elements, int index) {
198+
void check(List<ElementImpl>? elements, int index) {
243199
if (elements != null && elements.length != index) {
244200
throw StateError(
245201
'Unmatched ${elements[index].runtimeType} ${elements[index]}');
@@ -257,5 +213,5 @@ class ElementWalker {
257213
check(_variables, _variableIndex);
258214
}
259215

260-
static bool _isNotSynthetic(Element e) => !e.isSynthetic;
216+
static bool _isNotSynthetic(ElementImpl e) => !e.isSynthetic;
261217
}

0 commit comments

Comments
 (0)