@@ -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+
282342sealed 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