Skip to content

Commit 0dde212

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Add LookupResult
This replaces the individual lookups of getables and setables in LookupScope and NameSpace with single lookup that returns a LookupResult holding both the getable and the setable. This prepares for having getter/setter pairs in the same SourcePropertyBuilder and avoids the need for AccessErrorBuilder for handling lookups of getters where only setters exist and vice versa. Change-Id: I2b1e2477ed43506d9f94c48acd4b44277b490540 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/420080 Reviewed-by: Chloe Stefantsova <[email protected]> Reviewed-by: Erik Ernst <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 7658151 commit 0dde212

File tree

119 files changed

+6525
-1296
lines changed

Some content is hidden

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

119 files changed

+6525
-1296
lines changed

pkg/front_end/lib/src/base/incremental_compiler.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
694694
if (sourceBuilder == null) {
695695
sourceBuilder = sourceLibraryBuilder.exportNameSpace
696696
.lookupLocalMember(name, setter: false);
697-
if (sourceBuilder is MemberBuilder &&
698-
sourceBuilder.isField &&
699-
sourceBuilder.isAssignable) {
697+
if (sourceBuilder is MemberBuilder && sourceBuilder.hasSetter) {
700698
// Assignable fields can be lowered into a getter and setter.
701699
return;
702700
}

pkg/front_end/lib/src/base/local_scope.dart

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
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-
import '../builder/builder.dart';
65
import '../builder/declaration_builders.dart';
76
import '../builder/variable_builder.dart';
7+
import 'lookup_result.dart';
88
import 'scope.dart';
99

1010
abstract class LocalScope implements LookupScope {
@@ -16,12 +16,12 @@ abstract class LocalScope implements LookupScope {
1616

1717
LocalScope createNestedFixedScope(
1818
{required String debugName,
19-
required Map<String, Builder> local,
19+
required Map<String, VariableBuilder> local,
2020
required ScopeKind kind});
2121

22-
Iterable<Builder> get localVariables;
22+
Iterable<VariableBuilder> get localVariables;
2323

24-
Builder? lookupLocalVariable(String name);
24+
VariableBuilder? lookupLocalVariable(String name);
2525

2626
/// Declares that the meaning of [name] in this scope is [builder].
2727
///
@@ -30,12 +30,6 @@ abstract class LocalScope implements LookupScope {
3030
/// [name] being used before its declared.
3131
List<int>? declare(String name, VariableBuilder builder);
3232

33-
@override
34-
Builder? lookupGetable(String name, int charOffset, Uri fileUri);
35-
36-
@override
37-
Builder? lookupSetable(String name, int charOffset, Uri uri);
38-
3933
Map<String, List<int>>? get usedNames;
4034
}
4135

@@ -49,53 +43,32 @@ abstract base class BaseLocalScope implements LocalScope {
4943
@override
5044
LocalScope createNestedFixedScope(
5145
{required String debugName,
52-
required Map<String, Builder> local,
46+
required Map<String, VariableBuilder> local,
5347
required ScopeKind kind}) {
5448
return new FixedLocalScope(
5549
kind: kind, parent: this, local: local, debugName: debugName);
5650
}
5751
}
5852

59-
mixin LocalScopeMixin implements LookupScopeMixin, LocalScope {
53+
mixin LocalScopeMixin implements LocalScope {
6054
LookupScope? get _parent;
6155

62-
Map<String, Builder>? get _local;
63-
64-
@override
65-
String get classNameOrDebugName;
56+
Map<String, VariableBuilder>? get _local;
6657

6758
@override
68-
Iterable<Builder> get localVariables => _local?.values ?? const {};
59+
Iterable<VariableBuilder> get localVariables => _local?.values ?? const [];
6960

7061
@override
71-
Builder? lookupGetable(String name, int charOffset, Uri fileUri) {
72-
_recordUse(name, charOffset);
73-
if (_local != null) {
74-
Builder? builder = lookupGetableIn(name, charOffset, fileUri, _local!);
75-
if (builder != null) {
76-
return builder;
77-
}
78-
}
79-
return _parent?.lookupGetable(name, charOffset, fileUri);
62+
LookupResult? lookup(String name, int fileOffset, Uri fileUri) {
63+
_recordUse(name, fileOffset);
64+
return _local?[name] ?? _parent?.lookup(name, fileOffset, fileUri);
8065
}
8166

8267
@override
83-
Builder? lookupLocalVariable(String name) {
68+
VariableBuilder? lookupLocalVariable(String name) {
8469
return _local?[name];
8570
}
8671

87-
@override
88-
Builder? lookupSetable(String name, int charOffset, Uri fileUri) {
89-
_recordUse(name, charOffset);
90-
if (_local != null) {
91-
Builder? builder = lookupSetableIn(name, charOffset, fileUri, _local);
92-
if (builder != null) {
93-
return builder;
94-
}
95-
}
96-
return _parent?.lookupSetable(name, charOffset, fileUri);
97-
}
98-
9972
void _recordUse(String name, int charOffset) {}
10073

10174
@override
@@ -106,13 +79,12 @@ mixin LocalScopeMixin implements LookupScopeMixin, LocalScope {
10679
}
10780

10881
final class LocalScopeImpl extends BaseLocalScope
109-
with LookupScopeMixin, LocalScopeMixin
82+
with LocalScopeMixin
11083
implements LocalScope {
11184
@override
11285
final LocalScope? _parent;
11386

114-
@override
115-
final String classNameOrDebugName;
87+
final String _debugName;
11688

11789
/// Names declared in this scope.
11890
@override
@@ -124,7 +96,7 @@ final class LocalScopeImpl extends BaseLocalScope
12496
@override
12597
final ScopeKind kind;
12698

127-
LocalScopeImpl(this._parent, this.kind, this.classNameOrDebugName);
99+
LocalScopeImpl(this._parent, this.kind, this._debugName);
128100

129101
@override
130102
List<int>? declare(String name, VariableBuilder builder) {
@@ -145,8 +117,7 @@ final class LocalScopeImpl extends BaseLocalScope
145117
}
146118

147119
@override
148-
String toString() =>
149-
"$runtimeType(${kind}, $classNameOrDebugName, ${_local?.keys})";
120+
String toString() => "$runtimeType(${kind}, $_debugName, ${_local?.keys})";
150121
}
151122

152123
mixin ImmutableLocalScopeMixin implements LocalScope {
@@ -161,12 +132,12 @@ mixin ImmutableLocalScopeMixin implements LocalScope {
161132
}
162133

163134
final class LocalTypeParameterScope extends BaseLocalScope
164-
with LookupScopeMixin, ImmutableLocalScopeMixin, LocalScopeMixin {
165-
@override
135+
with ImmutableLocalScopeMixin {
166136
final LocalScope? _parent;
137+
167138
@override
168139
final ScopeKind kind;
169-
@override
140+
170141
final Map<String, TypeParameterBuilder>? _local;
171142

172143
final String _debugName;
@@ -181,61 +152,70 @@ final class LocalTypeParameterScope extends BaseLocalScope
181152
_debugName = debugName;
182153

183154
@override
184-
String get classNameOrDebugName => _debugName;
155+
// Coverage-ignore(suite): Not run.
156+
Iterable<VariableBuilder> get localVariables => const [];
185157

186158
@override
187-
String toString() =>
188-
"$runtimeType(${kind}, $classNameOrDebugName, ${_local?.keys})";
159+
LookupResult? lookup(String name, int fileOffset, Uri fileUri) {
160+
return _local?[name] ?? _parent?.lookup(name, fileOffset, fileUri);
161+
}
162+
163+
@override
164+
// Coverage-ignore(suite): Not run.
165+
VariableBuilder? lookupLocalVariable(String name) => null;
166+
167+
@override
168+
// Coverage-ignore(suite): Not run.
169+
void forEachExtension(void Function(ExtensionBuilder) f) {
170+
_parent?.forEachExtension(f);
171+
}
172+
173+
@override
174+
String toString() => "$runtimeType(${kind}, $_debugName, ${_local?.keys})";
189175
}
190176

191177
final class FixedLocalScope extends BaseLocalScope
192-
with LookupScopeMixin, ImmutableLocalScopeMixin, LocalScopeMixin {
178+
with ImmutableLocalScopeMixin, LocalScopeMixin {
193179
@override
194180
final LocalScope? _parent;
195181
@override
196182
final ScopeKind kind;
197183
@override
198-
final Map<String, Builder>? _local;
184+
final Map<String, VariableBuilder>? _local;
199185

200186
final String _debugName;
201187

202188
FixedLocalScope(
203189
{required this.kind,
204190
LocalScope? parent,
205-
Map<String, Builder>? local,
191+
Map<String, VariableBuilder>? local,
206192
required String debugName})
207193
: _parent = parent,
208194
_local = local,
209195
_debugName = debugName;
210196

211197
@override
212-
String get classNameOrDebugName => _debugName;
213-
214-
@override
215-
String toString() =>
216-
"$runtimeType(${kind}, $classNameOrDebugName, ${_local?.keys})";
198+
String toString() => "$runtimeType(${kind}, $_debugName, ${_local?.keys})";
217199
}
218200

219201
final class FormalParameterScope extends BaseLocalScope
220-
with LookupScopeMixin, ImmutableLocalScopeMixin, LocalScopeMixin {
202+
with ImmutableLocalScopeMixin, LocalScopeMixin {
221203
@override
222204
final LookupScope? _parent;
223205
@override
224-
final Map<String, Builder>? _local;
206+
final Map<String, VariableBuilder>? _local;
225207

226-
FormalParameterScope({LookupScope? parent, Map<String, Builder>? local})
208+
FormalParameterScope(
209+
{LookupScope? parent, Map<String, VariableBuilder>? local})
227210
: _parent = parent,
228211
_local = local;
229212

230213
@override
231214
ScopeKind get kind => ScopeKind.formals;
232215

233-
@override
234-
String get classNameOrDebugName => "formal parameter";
235-
236216
@override
237217
String toString() =>
238-
"$runtimeType(${kind}, $classNameOrDebugName, ${_local?.keys})";
218+
"$runtimeType(${kind}, formal parameter, ${_local?.keys})";
239219
}
240220

241221
final class EnclosingLocalScope extends BaseLocalScope
@@ -249,21 +229,16 @@ final class EnclosingLocalScope extends BaseLocalScope
249229

250230
@override
251231
// Coverage-ignore(suite): Not run.
252-
Iterable<Builder> get localVariables => const [];
232+
Iterable<VariableBuilder> get localVariables => const [];
253233

254234
@override
255-
Builder? lookupGetable(String name, int charOffset, Uri fileUri) {
256-
return _scope.lookupGetable(name, charOffset, fileUri);
235+
LookupResult? lookup(String name, int fileOffset, Uri fileUri) {
236+
return _scope.lookup(name, fileOffset, fileUri);
257237
}
258238

259239
@override
260240
// Coverage-ignore(suite): Not run.
261-
Builder? lookupLocalVariable(String name) => null;
262-
263-
@override
264-
Builder? lookupSetable(String name, int charOffset, Uri uri) {
265-
return _scope.lookupSetable(name, charOffset, uri);
266-
}
241+
VariableBuilder? lookupLocalVariable(String name) => null;
267242

268243
@override
269244
// Coverage-ignore(suite): Not run.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import '../builder/builder.dart';
6+
7+
abstract class LookupResult {
8+
/// The [Builder] used for reading this entity, if any.
9+
Builder? get getable;
10+
11+
/// The [Builder] used for writing to this entity, if any.
12+
Builder? get setable;
13+
14+
static LookupResult? fromBuilders(Builder? getable, Builder? setable) {
15+
if (getable != null && setable != null) {
16+
return new GetableSetableResult(getable, setable);
17+
} else if (getable != null) {
18+
return new GetableResult(getable);
19+
} else if (setable != null) {
20+
return new SetableResult(setable);
21+
} else {
22+
return null;
23+
}
24+
}
25+
}
26+
27+
class GetableResult implements LookupResult {
28+
@override
29+
final Builder getable;
30+
31+
GetableResult(this.getable);
32+
33+
@override
34+
Builder? get setable => null;
35+
}
36+
37+
class SetableResult implements LookupResult {
38+
@override
39+
final Builder setable;
40+
41+
SetableResult(this.setable);
42+
43+
@override
44+
Builder? get getable => null;
45+
}
46+
47+
class GetableSetableResult implements LookupResult {
48+
@override
49+
final Builder getable;
50+
51+
@override
52+
final Builder setable;
53+
54+
GetableSetableResult(this.getable, this.setable);
55+
}

0 commit comments

Comments
 (0)