Skip to content

Commit ae92595

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] MethodEncodingStrategy and PropertyEncodingStrategy
Change-Id: I4ff0ebffd3cb5511771b777e98a01ffd609a57ce Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405341 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent ca540d8 commit ae92595

File tree

9 files changed

+500
-355
lines changed

9 files changed

+500
-355
lines changed

pkg/front_end/lib/src/fragment/fragment.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import 'package:kernel/transformations/flags.dart';
1212
import 'package:kernel/type_algebra.dart';
1313
import 'package:kernel/type_environment.dart';
1414

15-
import '../api_prototype/codes.dart';
1615
import '../api_prototype/lowering_predicates.dart';
1716
import '../base/constant_context.dart';
1817
import '../base/local_scope.dart';
18+
import '../base/messages.dart';
1919
import '../base/modifiers.dart';
2020
import '../base/problems.dart';
2121
import '../base/scope.dart';
@@ -41,6 +41,7 @@ import '../kernel/late_lowering.dart' as late_lowering;
4141
import '../kernel/macro/metadata.dart';
4242
import '../kernel/member_covariance.dart';
4343
import '../kernel/type_algorithms.dart';
44+
import '../source/builder_factory.dart';
4445
import '../source/name_scheme.dart';
4546
import '../source/source_class_builder.dart';
4647
import '../source/source_constructor_builder.dart';

pkg/front_end/lib/src/fragment/getter.dart

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,50 +80,17 @@ class GetterFragment implements Fragment, FunctionFragment {
8080
}
8181

8282
void setBuilder(
83+
ProblemReporting problemReporting,
8384
SourcePropertyBuilder value,
84-
List<NominalParameterBuilder>? typeParameters,
85-
List<FormalParameterBuilder>? formals) {
85+
PropertyEncodingStrategy encodingStrategy,
86+
List<NominalParameterBuilder> unboundNominalParameters) {
8687
assert(_builder == null, "Builder has already been computed for $this.");
8788
_builder = value;
88-
switch (value.declarationBuilder) {
89-
case null:
90-
case ClassBuilder():
91-
_encoding = new _RegularGetterEncoding(this);
92-
case ExtensionTypeDeclarationBuilder():
93-
if (modifiers.isStatic) {
94-
assert(typeParameters == null,
95-
"Unexpected type parameters on getter: $typeParameters");
96-
assert(formals == null,
97-
"Unexpected formal parameters on getter: $formals");
98-
_encoding = new _ExtensionTypeStaticGetterEncoding(this);
99-
} else {
100-
assert(
101-
formals != null,
102-
"Unexpected formal parameters on extension type instance getter: "
103-
"$formals");
104-
assert(formals!.length == 1,
105-
"Unexpected formals on extension type instance getter: $formals");
106-
_encoding = new _ExtensionTypeInstanceGetterEncoding(
107-
this, typeParameters, formals!.single);
108-
}
109-
case ExtensionBuilder():
110-
if (modifiers.isStatic) {
111-
assert(typeParameters == null,
112-
"Unexpected type parameters on getter: $typeParameters");
113-
assert(formals == null,
114-
"Unexpected formal parameters on getter: $formals");
115-
_encoding = new _ExtensionStaticGetterEncoding(this);
116-
} else {
117-
assert(
118-
formals != null,
119-
"Unexpected formal parameters on extension instance getter: "
120-
"$formals");
121-
assert(formals!.length == 1,
122-
"Unexpected formals on extension instance getter: $formals");
123-
_encoding = new _ExtensionInstanceGetterEncoding(
124-
this, typeParameters, formals!.single);
125-
}
126-
}
89+
_encoding = encodingStrategy.createGetterEncoding(
90+
value, this, unboundNominalParameters);
91+
typeParameterNameSpace.addTypeParameters(
92+
problemReporting, _encoding.clonedAndDeclaredTypeParameters,
93+
ownerName: name, allowNameConflict: true);
12794
returnType.registerInferredTypeListener(_encoding);
12895
}
12996

@@ -208,7 +175,7 @@ class GetterFragment implements Fragment, FunctionFragment {
208175

209176
// Coverage-ignore(suite): Not run.
210177
List<NominalParameterBuilder>? get typeParametersForTesting =>
211-
_encoding.typeParametersForTesting;
178+
_encoding.clonedAndDeclaredTypeParameters;
212179

213180
// Coverage-ignore(suite): Not run.
214181
List<FormalParameterBuilder>? get formalsForTesting =>
@@ -304,7 +271,7 @@ sealed class _GetterEncoding implements InferredTypeListener {
304271
void checkVariance(
305272
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment);
306273

307-
List<NominalParameterBuilder>? get typeParametersForTesting;
274+
List<NominalParameterBuilder>? get clonedAndDeclaredTypeParameters;
308275

309276
List<FormalParameterBuilder>? get formalsForTesting;
310277
}
@@ -484,7 +451,7 @@ mixin _DirectGetterEncodingMixin implements _GetterEncoding {
484451

485452
@override
486453
// Coverage-ignore(suite): Not run.
487-
List<NominalParameterBuilder>? get typeParametersForTesting =>
454+
List<NominalParameterBuilder>? get clonedAndDeclaredTypeParameters =>
488455
_fragment.declaredTypeParameters;
489456

490457
@override
@@ -776,7 +743,7 @@ mixin _ExtensionInstanceGetterEncodingMixin implements _GetterEncoding {
776743

777744
@override
778745
// Coverage-ignore(suite): Not run.
779-
List<NominalParameterBuilder>? get typeParametersForTesting =>
746+
List<NominalParameterBuilder>? get clonedAndDeclaredTypeParameters =>
780747
_clonedDeclarationTypeParameters != null ||
781748
_fragment.declaredTypeParameters != null
782749
? [

pkg/front_end/lib/src/fragment/method.dart

Lines changed: 121 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -77,66 +77,17 @@ class MethodFragment implements Fragment, FunctionFragment {
7777
}
7878

7979
void setBuilder(
80+
ProblemReporting problemReporting,
8081
SourceMethodBuilder value,
81-
List<NominalParameterBuilder>? typeParameters,
82-
List<FormalParameterBuilder>? formals) {
82+
MethodEncodingStrategy encodingStrategy,
83+
List<NominalParameterBuilder> unboundNominalParameters) {
8384
assert(_builder == null, "Builder has already been computed for $this.");
8485
_builder = value;
85-
switch (value.declarationBuilder) {
86-
case null:
87-
case ClassBuilder():
88-
if (isOperator) {
89-
_encoding = new _RegularOperatorEncoding(this);
90-
} else {
91-
_encoding = new _RegularMethodEncoding(this);
92-
}
93-
case ExtensionTypeDeclarationBuilder():
94-
if (modifiers.isStatic) {
95-
assert(typeParameters == null,
96-
"Unexpected type parameters on getter: $typeParameters");
97-
assert(formals == null,
98-
"Unexpected formal parameters on getter: $formals");
99-
assert(!isOperator);
100-
_encoding = new _ExtensionTypeStaticMethodEncoding(this);
101-
} else {
102-
assert(
103-
formals != null,
104-
"Unexpected formal parameters on extension type instance getter: "
105-
"$formals");
106-
assert(formals!.length == 1,
107-
"Unexpected formals on extension type instance getter: $formals");
108-
if (isOperator) {
109-
_encoding = new _ExtensionTypeInstanceOperatorEncoding(
110-
this, typeParameters, formals!.single);
111-
} else {
112-
_encoding = new _ExtensionTypeInstanceMethodEncoding(
113-
this, typeParameters, formals!.single);
114-
}
115-
}
116-
case ExtensionBuilder():
117-
if (modifiers.isStatic) {
118-
assert(typeParameters == null,
119-
"Unexpected type parameters on getter: $typeParameters");
120-
assert(formals == null,
121-
"Unexpected formal parameters on getter: $formals");
122-
assert(!isOperator);
123-
_encoding = new _ExtensionStaticMethodEncoding(this);
124-
} else {
125-
assert(
126-
formals != null,
127-
"Unexpected formal parameters on extension instance method: "
128-
"$formals");
129-
assert(formals!.length == 1,
130-
"Unexpected formals on extension instance getter: $formals");
131-
if (isOperator) {
132-
_encoding = new _ExtensionInstanceOperatorEncoding(
133-
this, typeParameters, formals!.single);
134-
} else {
135-
_encoding = new _ExtensionInstanceMethodEncoding(
136-
this, typeParameters, formals!.single);
137-
}
138-
}
139-
}
86+
_encoding = encodingStrategy.createMethodEncoding(
87+
value, this, unboundNominalParameters);
88+
typeParameterNameSpace.addTypeParameters(
89+
problemReporting, _encoding.clonedAndDeclaredTypeParameters,
90+
ownerName: name, allowNameConflict: true);
14091
returnType.registerInferredTypeListener(_encoding);
14192
}
14293

@@ -224,7 +175,7 @@ class MethodFragment implements Fragment, FunctionFragment {
224175

225176
// Coverage-ignore(suite): Not run.
226177
List<NominalParameterBuilder>? get typeParametersForTesting =>
227-
_encoding.typeParametersForTesting;
178+
_encoding.clonedAndDeclaredTypeParameters;
228179

229180
// Coverage-ignore(suite): Not run.
230181
List<FormalParameterBuilder>? get formalsForTesting =>
@@ -279,6 +230,115 @@ class _MethodBodyBuildingContext implements FunctionBodyBuildingContext {
279230
VariableDeclaration? get thisVariable => _fragment._encoding.thisVariable;
280231
}
281232

233+
sealed class MethodEncodingStrategy {
234+
factory MethodEncodingStrategy(DeclarationBuilder? declarationBuilder,
235+
{required bool isInstanceMember}) {
236+
switch (declarationBuilder) {
237+
case ExtensionBuilder():
238+
if (isInstanceMember) {
239+
return const _ExtensionInstanceMethodStrategy();
240+
} else {
241+
return const _ExtensionStaticMethodStrategy();
242+
}
243+
case ExtensionTypeDeclarationBuilder():
244+
if (isInstanceMember) {
245+
return const _ExtensionTypeInstanceMethodStrategy();
246+
} else {
247+
return const _ExtensionTypeStaticMethodStrategy();
248+
}
249+
case null:
250+
case ClassBuilder():
251+
return const _RegularMethodStrategy();
252+
}
253+
}
254+
255+
_MethodEncoding createMethodEncoding(
256+
SourceMethodBuilder builder,
257+
MethodFragment fragment,
258+
List<NominalParameterBuilder> unboundNominalParameters);
259+
}
260+
261+
class _RegularMethodStrategy implements MethodEncodingStrategy {
262+
const _RegularMethodStrategy();
263+
264+
@override
265+
_MethodEncoding createMethodEncoding(
266+
SourceMethodBuilder builder,
267+
MethodFragment fragment,
268+
List<NominalParameterBuilder> unboundNominalParameters) {
269+
return fragment.isOperator
270+
? new _RegularOperatorEncoding(fragment)
271+
: new _RegularMethodEncoding(fragment);
272+
}
273+
}
274+
275+
class _ExtensionInstanceMethodStrategy implements MethodEncodingStrategy {
276+
const _ExtensionInstanceMethodStrategy();
277+
278+
@override
279+
_MethodEncoding createMethodEncoding(
280+
SourceMethodBuilder builder,
281+
MethodFragment fragment,
282+
List<NominalParameterBuilder> unboundNominalParameters) {
283+
ExtensionBuilder declarationBuilder =
284+
builder.declarationBuilder as ExtensionBuilder;
285+
SynthesizedExtensionSignature signature = new SynthesizedExtensionSignature(
286+
declarationBuilder, unboundNominalParameters,
287+
fileUri: fragment.fileUri, fileOffset: fragment.nameOffset);
288+
return fragment.isOperator
289+
? new _ExtensionInstanceOperatorEncoding(fragment,
290+
signature.clonedDeclarationTypeParameters, signature.thisFormal)
291+
: new _ExtensionInstanceMethodEncoding(fragment,
292+
signature.clonedDeclarationTypeParameters, signature.thisFormal);
293+
}
294+
}
295+
296+
class _ExtensionStaticMethodStrategy implements MethodEncodingStrategy {
297+
const _ExtensionStaticMethodStrategy();
298+
299+
@override
300+
_MethodEncoding createMethodEncoding(
301+
SourceMethodBuilder builder,
302+
MethodFragment fragment,
303+
List<NominalParameterBuilder> unboundNominalParameters) {
304+
return new _ExtensionStaticMethodEncoding(fragment);
305+
}
306+
}
307+
308+
class _ExtensionTypeInstanceMethodStrategy implements MethodEncodingStrategy {
309+
const _ExtensionTypeInstanceMethodStrategy();
310+
311+
@override
312+
_MethodEncoding createMethodEncoding(
313+
SourceMethodBuilder builder,
314+
MethodFragment fragment,
315+
List<NominalParameterBuilder> unboundNominalParameters) {
316+
ExtensionTypeDeclarationBuilder declarationBuilder =
317+
builder.declarationBuilder as ExtensionTypeDeclarationBuilder;
318+
SynthesizedExtensionTypeSignature signature =
319+
new SynthesizedExtensionTypeSignature(
320+
declarationBuilder, unboundNominalParameters,
321+
fileUri: fragment.fileUri, fileOffset: fragment.nameOffset);
322+
return fragment.isOperator
323+
? new _ExtensionTypeInstanceOperatorEncoding(fragment,
324+
signature.clonedDeclarationTypeParameters, signature.thisFormal)
325+
: new _ExtensionTypeInstanceMethodEncoding(fragment,
326+
signature.clonedDeclarationTypeParameters, signature.thisFormal);
327+
}
328+
}
329+
330+
class _ExtensionTypeStaticMethodStrategy implements MethodEncodingStrategy {
331+
const _ExtensionTypeStaticMethodStrategy();
332+
333+
@override
334+
_MethodEncoding createMethodEncoding(
335+
SourceMethodBuilder builder,
336+
MethodFragment fragment,
337+
List<NominalParameterBuilder> unboundNominalParameters) {
338+
return new _ExtensionTypeStaticMethodEncoding(fragment);
339+
}
340+
}
341+
282342
sealed class _MethodEncoding implements InferredTypeListener {
283343
VariableDeclaration? get thisVariable;
284344
List<TypeParameter>? get thisTypeParameters;
@@ -325,7 +385,7 @@ sealed class _MethodEncoding implements InferredTypeListener {
325385
void checkVariance(
326386
SourceClassBuilder sourceClassBuilder, TypeEnvironment typeEnvironment);
327387

328-
List<NominalParameterBuilder>? get typeParametersForTesting;
388+
List<NominalParameterBuilder>? get clonedAndDeclaredTypeParameters;
329389

330390
List<FormalParameterBuilder>? get formalsForTesting;
331391
}
@@ -516,7 +576,7 @@ mixin _DirectMethodEncodingMixin implements _MethodEncoding {
516576

517577
@override
518578
// Coverage-ignore(suite): Not run.
519-
List<NominalParameterBuilder>? get typeParametersForTesting =>
579+
List<NominalParameterBuilder>? get clonedAndDeclaredTypeParameters =>
520580
_fragment.declaredTypeParameters;
521581

522582
@override
@@ -1034,7 +1094,7 @@ mixin _ExtensionInstanceMethodEncodingMixin implements _MethodEncoding {
10341094

10351095
@override
10361096
// Coverage-ignore(suite): Not run.
1037-
List<NominalParameterBuilder>? get typeParametersForTesting =>
1097+
List<NominalParameterBuilder>? get clonedAndDeclaredTypeParameters =>
10381098
_clonedDeclarationTypeParameters != null ||
10391099
_fragment.declaredTypeParameters != null
10401100
? [

0 commit comments

Comments
 (0)