Skip to content

Commit adaa736

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[cfe] Implement static extension getters in the CFE
Part of #61484 Part of #61486 Change-Id: I4918027c164d590ff42ef36e7301a1939589040c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452145 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent 1188d01 commit adaa736

17 files changed

+215
-16
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ enum ScopeKind {
121121
import,
122122
}
123123

124-
abstract class LookupScope {
124+
abstract class ExtensionScope {
125+
void forEachExtension(void Function(ExtensionBuilder) f);
126+
}
127+
128+
abstract class LookupScope implements ExtensionScope {
125129
ScopeKind get kind;
126130
LookupResult? lookup(String name);
127-
// TODO(johnniwinther): Should this be moved to an outer scope interface?
128-
void forEachExtension(void Function(ExtensionBuilder) f);
129131
}
130132

131133
/// A [LookupScope] based directly on a [NameSpace].

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ class BodyBuilderImpl extends StackListenerImpl
349349
/// Index for building unique lowered names for wildcard variables.
350350
int wildcardVariableIndex = 0;
351351

352+
@override
353+
ExtensionScope extensionScope;
354+
352355
BodyBuilderImpl({
353356
required this.libraryBuilder,
354357
required BodyBuilderContext context,
@@ -362,6 +365,7 @@ class BodyBuilderImpl extends StackListenerImpl
362365
required this.assignedVariables,
363366
required this.typeEnvironment,
364367
required ConstantContext constantContext,
368+
required this.extensionScope,
365369
}) : _context = context,
366370
forest = const Forest(),
367371
enableNative = libraryBuilder.loader.target.backendTarget.enableNative(

pkg/front_end/lib/src/kernel/expression_generator.dart

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4461,12 +4461,38 @@ class TypeUseGenerator extends AbstractReadOnlyAccessGenerator {
44614461
return tearOffExpression;
44624462
}
44634463
}
4464-
generator = new UnresolvedNameGenerator(
4465-
_helper,
4466-
send.token,
4467-
name,
4468-
unresolvedReadKind: UnresolvedKind.Member,
4469-
);
4464+
4465+
MemberBuilder? extensionMemberBuilder;
4466+
4467+
if (_helper.libraryFeatures.staticExtensions.isEnabled) {
4468+
_helper.extensionScope.forEachExtension((
4469+
ExtensionBuilder extensionBuilder,
4470+
) {
4471+
if (extensionBuilder.onType.declaration == declaration) {
4472+
extensionMemberBuilder = extensionBuilder
4473+
.lookupLocalMemberByName(name, setter: false);
4474+
}
4475+
});
4476+
}
4477+
4478+
if (extensionMemberBuilder != null) {
4479+
generator = new StaticAccessGenerator.fromBuilder(
4480+
_helper,
4481+
name,
4482+
send.token,
4483+
extensionMemberBuilder,
4484+
null,
4485+
typeOffset: fileOffset,
4486+
isNullAware: isNullAware,
4487+
);
4488+
} else {
4489+
generator = new UnresolvedNameGenerator(
4490+
_helper,
4491+
send.token,
4492+
name,
4493+
unresolvedReadKind: UnresolvedKind.Member,
4494+
);
4495+
}
44704496
} else {
44714497
return _helper.resolveAndBuildConstructorInvocation(
44724498
declarationBuilder,

pkg/front_end/lib/src/kernel/expression_generator_helper.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ abstract class ExpressionGeneratorHelper {
5858

5959
CompilerContext get compilerContext;
6060

61+
ExtensionScope get extensionScope;
62+
6163
InvalidExpression buildProblem({
6264
required Message message,
6365
required Uri fileUri,

pkg/front_end/lib/src/kernel/resolver.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ class Resolver {
944944
libraryBuilder: context.libraryBuilder,
945945
context: bodyBuilderContext,
946946
enclosingScope: new EnclosingLocalScope(scope),
947+
extensionScope: context.typeInferrer.extensionScope,
947948
formalParameterScope: formalParameterScope,
948949
hierarchy: _classHierarchy,
949950
coreTypes: _coreTypes,

pkg/front_end/lib/src/type_inference/inference_visitor_base.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
190190

191191
ProblemReportingHelper get problemReportingHelper => libraryBuilder.loader;
192192

193-
LookupScope get extensionScope => _inferrer.extensionScope;
193+
ExtensionScope get extensionScope => _inferrer.extensionScope;
194194

195195
bool get isInferenceUpdate1Enabled =>
196196
libraryBuilder.isInferenceUpdate1Enabled;

pkg/front_end/lib/src/type_inference/type_inferrer.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ abstract class TypeInferrer {
3333
/// Gets the [TypeSchemaEnvironment] being used for type inference.
3434
TypeSchemaEnvironment get typeSchemaEnvironment;
3535

36+
/// Gets the [ExtensionScope] being used for type inference.
37+
ExtensionScope get extensionScope;
38+
3639
/// Returns the [FlowAnalysis] used during inference.
3740
FlowAnalysis<
3841
TreeNode,
@@ -134,7 +137,8 @@ class TypeInferrerImpl implements TypeInferrer {
134137

135138
final SourceLibraryBuilder libraryBuilder;
136139

137-
final LookupScope extensionScope;
140+
@override
141+
final ExtensionScope extensionScope;
138142

139143
late final StaticTypeContext staticTypeContext =
140144
new StaticTypeContextImpl.direct(
@@ -381,11 +385,14 @@ class TypeInferrerImplBenchmarked implements TypeInferrer {
381385
final TypeInferrerImpl impl;
382386
final Benchmarker benchmarker;
383387

388+
@override
389+
final ExtensionScope extensionScope;
390+
384391
TypeInferrerImplBenchmarked(
385392
TypeInferenceEngine engine,
386393
InterfaceType? thisType,
387394
SourceLibraryBuilder libraryBuilder,
388-
LookupScope extensionScope,
395+
this.extensionScope,
389396
AssignedVariables<TreeNode, VariableDeclaration> assignedVariables,
390397
InferenceDataForTesting? dataForTesting,
391398
this.benchmarker,

pkg/front_end/test/compiler_test_helper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class BodyBuilderTest extends BodyBuilderImpl {
301301
libraryBuilder: libraryBuilder,
302302
context: context,
303303
enclosingScope: new EnclosingLocalScope(enclosingScope),
304+
extensionScope: enclosingScope,
304305
formalParameterScope: formalParameterScope,
305306
hierarchy: hierarchy,
306307
coreTypes: coreTypes,

pkg/front_end/test/generator_to_string_test.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,16 @@ Future<void> main() async {
187187
extensionScope: compilationUnit.compilationUnitScope,
188188
);
189189

190+
LocalScope lookupScope = new FixedLocalScope(
191+
kind: ScopeKind.library,
192+
debugName: "dummy",
193+
);
190194
ExpressionGeneratorHelper helper = new BodyBuilderImpl(
191195
libraryBuilder: libraryBuilder,
192196
context: new LibraryBodyBuilderContext(libraryBuilder),
193197
uri: uri,
194-
enclosingScope: new FixedLocalScope(
195-
kind: ScopeKind.library,
196-
debugName: "dummy",
197-
),
198+
enclosingScope: lookupScope,
199+
extensionScope: lookupScope,
198200
coreTypes: coreTypes,
199201
hierarchy: hierarchy,
200202
assignedVariables: typeInferrer.assignedVariables,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--enable-experiment=static-extensions

0 commit comments

Comments
 (0)