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- // @dart = 2.10
6-
75library dart2js.js_emitter.parameter_stub_generator;
86
97import '../common/elements.dart' show JElementEnvironment;
@@ -23,10 +21,9 @@ import '../universe/call_structure.dart' show CallStructure;
2321import '../universe/codegen_world_builder.dart' ;
2422import '../universe/selector.dart' show Selector;
2523import '../universe/world_builder.dart' show SelectorConstraints;
26-
2724import 'model.dart' ;
28-
29- import 'interfaces .dart' show Emitter, NativeEmitter ;
25+ import 'interfaces.dart' show Emitter;
26+ import 'native_emitter .dart' ;
3027
3128class ParameterStubGenerator {
3229 static final Set <Selector > emptySelectorSet = {};
@@ -57,6 +54,9 @@ class ParameterStubGenerator {
5754 bool needsSuperGetter (FunctionEntity element) =>
5855 _codegenWorld.methodsNeedsSuperGetter (element);
5956
57+ static final jsAst.Parameter _placeholderParam = jsAst.Parameter ('_' );
58+ static final jsAst.Expression _placeholderExpression = jsAst.LiteralNull ();
59+
6060 /// Generates stubs to fill in missing optional named or positional arguments
6161 /// and missing type arguments. Returns `null` if no stub is needed.
6262 ///
@@ -70,8 +70,8 @@ class ParameterStubGenerator {
7070 /// method has the corresponding name [ParameterStubMethod.name] and
7171 /// [ParameterStubMethod.callName] set if the input selector is non-null (and
7272 /// the member needs a stub).
73- ParameterStubMethod generateParameterStub (
74- FunctionEntity member, Selector selector, Selector callSelector) {
73+ ParameterStubMethod ? generateParameterStub (
74+ FunctionEntity member, Selector selector, Selector ? callSelector) {
7575 // The naming here can be a bit confusing. There is a call site somewhere
7676 // that calls the stub via the [selector], which has a [CallStructure], so
7777 // the *Call*Structure determines the *parameters* of the stub. The body of
@@ -113,17 +113,17 @@ class ParameterStubGenerator {
113113 String receiverArgumentName = r'$receiver' ;
114114
115115 // The parameters that this stub takes.
116- List <jsAst.Parameter > stubParameters = List <jsAst. Parameter > .filled (
116+ List <jsAst.Parameter > stubParameters = List .filled (
117117 extraArgumentCount +
118118 selector.argumentCount +
119119 selector.typeArgumentCount,
120- null );
120+ _placeholderParam );
121121 // The arguments that will be passed to the real method.
122- List <jsAst.Expression > targetArguments = List <jsAst. Expression > .filled (
122+ List <jsAst.Expression > targetArguments = List .filled (
123123 extraArgumentCount +
124124 parameterStructure.totalParameters +
125125 parameterStructure.typeParameters,
126- null );
126+ _placeholderExpression );
127127
128128 int count = 0 ;
129129 if (isInterceptedMethod) {
@@ -137,8 +137,8 @@ class ParameterStubGenerator {
137137 int indexOfLastOptionalArgumentInParameters = optionalParameterStart - 1 ;
138138
139139 _elementEnvironment.forEachParameter (member,
140- (_, String name, ConstantValue value) {
141- String jsName = _namer.safeVariableName (name);
140+ (_, String ? name, ConstantValue ? value) {
141+ String jsName = _namer.safeVariableName (name! );
142142 assert (jsName != receiverArgumentName);
143143 if (count < optionalParameterStart) {
144144 stubParameters[count] = jsAst.Parameter (jsName);
@@ -188,6 +188,9 @@ class ParameterStubGenerator {
188188 }
189189 }
190190
191+ assert (! stubParameters.any ((e) => identical (e, _placeholderParam)));
192+ assert (! targetArguments.any ((e) => identical (e, _placeholderExpression)));
193+
191194 var body; // List or jsAst.Statement.
192195 if (_nativeData.hasFixedBackendName (member)) {
193196 body = _nativeEmitter.generateParameterStubStatements (
@@ -199,7 +202,7 @@ class ParameterStubGenerator {
199202 indexOfLastOptionalArgumentInParameters);
200203 } else if (member.isInstanceMember) {
201204 if (needsSuperGetter (member)) {
202- ClassEntity superClass = member.enclosingClass;
205+ final superClass = member.enclosingClass! ;
203206 jsAst.Name methodName = _namer.instanceMethodName (member);
204207 // When redirecting, we must ensure that we don't end up in a subclass.
205208 // We thus can't just invoke `this.foo$1.call(filledInArguments)`.
@@ -221,14 +224,14 @@ class ParameterStubGenerator {
221224
222225 SourceInformationBuilder sourceInformationBuilder =
223226 _sourceInformationStrategy.createBuilderForContext (member);
224- SourceInformation sourceInformation =
227+ final sourceInformation =
225228 sourceInformationBuilder.buildStub (member, callStructure);
226229
227- jsAst. Fun function = js ('function(#) { #; }' , [stubParameters, body])
228- .withSourceInformation (sourceInformation);
230+ final function = js ('function(#) { #; }' , [stubParameters, body])
231+ .withSourceInformation (sourceInformation) as jsAst. Fun ;
229232
230- jsAst. Name name = member.isStatic ? null : _namer.invocationName (selector);
231- jsAst. Name callName =
233+ final name = member.isStatic ? null : _namer.invocationName (selector);
234+ final callName =
232235 (callSelector != null ) ? _namer.invocationName (callSelector) : null ;
233236 return ParameterStubMethod (name, callName, function, element: member);
234237 }
@@ -274,36 +277,31 @@ class ParameterStubGenerator {
274277 // (2) foo$3$c(a, b, c) => MyClass.foo$4$c$d(this, a, b, c, null);
275278 // (3) foo$3$d(a, b, d) => MyClass.foo$4$c$d(this, a, b, null, d);
276279 List <ParameterStubMethod > generateParameterStubs (FunctionEntity member,
277- {bool canTearOff, bool canBeApplied}) {
278- assert (canTearOff != null );
279- assert (canBeApplied != null );
280+ {required bool canTearOff, required bool canBeApplied}) {
280281 // The set of selectors that apply to `member`. For example, for
281282 // a member `foo(x, [y])` the following selectors may apply:
282283 // `foo(x)`, and `foo(x, y)`.
283- Map <Selector , SelectorConstraints > liveSelectors;
284+ final Map <Selector , SelectorConstraints > liveSelectors =
285+ // Only instance members (not static methods) need stubs.
286+ (member.isInstanceMember
287+ ? _codegenWorld.invocationsByName (member.name! )
288+ : const {}) ??
289+ const {};
284290 // The set of selectors that apply to `member` if it's name was `call`.
285291 // This happens when a member is torn off. In that case calls to the
286292 // function use the name `call`, and we must be able to handle every
287293 // `call` invocation that matches the signature. For example, for
288294 // a member `foo(x, [y])` the following selectors would be possible
289295 // call-selectors: `call(x)`, and `call(x, y)`.
290- Map <Selector , SelectorConstraints > callSelectors;
296+ final Map <Selector , SelectorConstraints > callSelectors = (canTearOff
297+ ? _codegenWorld
298+ .invocationsByName (_namer.closureInvocationSelectorName)
299+ : const {}) ??
300+ const {};
291301
292302 int memberTypeParameters = member.parameterStructure.typeParameters;
293303
294- // Only instance members (not static methods) need stubs.
295- if (member.isInstanceMember) {
296- liveSelectors = _codegenWorld.invocationsByName (member.name);
297- }
298-
299- if (canTearOff) {
300- String call = _namer.closureInvocationSelectorName;
301- callSelectors = _codegenWorld.invocationsByName (call);
302- }
303-
304304 assert (emptySelectorSet.isEmpty);
305- liveSelectors ?? = const {};
306- callSelectors ?? = const {};
307305
308306 List <ParameterStubMethod > stubs = [];
309307
@@ -334,9 +332,8 @@ class ParameterStubGenerator {
334332
335333 renamedCallSelectors.add (namedSelector);
336334 stubSelectors.add (namedSelector);
337- ParameterStubMethod stub =
338- generateParameterStub (member, namedSelector, closureSelector);
339- assert (stub != null );
335+ final stub =
336+ generateParameterStub (member, namedSelector, closureSelector)! ;
340337 stubs.add (stub);
341338 }
342339
@@ -350,8 +347,7 @@ class ParameterStubGenerator {
350347 }
351348
352349 if (stubSelectors.add (renamedSelector)) {
353- ParameterStubMethod stub =
354- generateParameterStub (member, renamedSelector, selector);
350+ final stub = generateParameterStub (member, renamedSelector, selector);
355351 if (stub != null ) {
356352 stubs.add (stub);
357353 }
@@ -374,7 +370,7 @@ class ParameterStubGenerator {
374370 if (stubSelectors.add (renamedSelectorWithTypeArguments)) {
375371 Selector closureSelector =
376372 Selector .callClosureFrom (renamedSelectorWithTypeArguments);
377- ParameterStubMethod stub = generateParameterStub (
373+ final stub = generateParameterStub (
378374 member, renamedSelectorWithTypeArguments, closureSelector);
379375 if (stub != null ) {
380376 stubs.add (stub);
@@ -390,14 +386,13 @@ class ParameterStubGenerator {
390386 for (Selector selector in liveSelectors.keys) {
391387 if (renamedCallSelectors.contains (selector)) continue ;
392388 if (! selector.appliesUnnamed (member)) continue ;
393- if (! liveSelectors[selector]
389+ if (! liveSelectors[selector]!
394390 .canHit (member, selector.memberName, _closedWorld)) {
395391 continue ;
396392 }
397393
398394 if (stubSelectors.add (selector)) {
399- ParameterStubMethod stub =
400- generateParameterStub (member, selector, null );
395+ final stub = generateParameterStub (member, selector, null );
401396 if (stub != null ) {
402397 stubs.add (stub);
403398 }
0 commit comments