| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class ParameterStubGenerator { | 7 class ParameterStubGenerator { |
| 8 static final Set<Selector> emptySelectorSet = new Set<Selector>(); | 8 static final Set<Selector> emptySelectorSet = new Set<Selector>(); |
| 9 | 9 |
| 10 final Namer namer; | 10 final Namer namer; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 * | 31 * |
| 32 * Members may be invoked in two ways: directly, or through a closure. In the | 32 * Members may be invoked in two ways: directly, or through a closure. In the |
| 33 * latter case the caller invokes the closure's `call` method. This method | 33 * latter case the caller invokes the closure's `call` method. This method |
| 34 * accepts two selectors. The returned stub method has the corresponding | 34 * accepts two selectors. The returned stub method has the corresponding |
| 35 * name [ParameterStubMethod.name] and [ParameterStubMethod.callName] set if | 35 * name [ParameterStubMethod.name] and [ParameterStubMethod.callName] set if |
| 36 * the input selector is non-null (and the member needs a stub). | 36 * the input selector is non-null (and the member needs a stub). |
| 37 */ | 37 */ |
| 38 ParameterStubMethod generateParameterStub(FunctionElement member, | 38 ParameterStubMethod generateParameterStub(FunctionElement member, |
| 39 Selector selector, | 39 Selector selector, |
| 40 Selector callSelector) { | 40 Selector callSelector) { |
| 41 CallStructure callStructure = selector.callStructure; | |
| 42 FunctionSignature parameters = member.functionSignature; | 41 FunctionSignature parameters = member.functionSignature; |
| 43 int positionalArgumentCount = callStructure.positionalArgumentCount; | 42 int positionalArgumentCount = selector.positionalArgumentCount; |
| 44 if (positionalArgumentCount == parameters.parameterCount) { | 43 if (positionalArgumentCount == parameters.parameterCount) { |
| 45 assert(callStructure.isUnnamed); | 44 assert(selector.namedArgumentCount == 0); |
| 46 return null; | 45 return null; |
| 47 } | 46 } |
| 48 if (parameters.optionalParametersAreNamed && | 47 if (parameters.optionalParametersAreNamed |
| 49 callStructure.namedArgumentCount == parameters.optionalParameterCount) { | 48 && selector.namedArgumentCount == parameters.optionalParameterCount) { |
| 50 // If the selector has the same number of named arguments as the element, | 49 // If the selector has the same number of named arguments as the element, |
| 51 // we don't need to add a stub. The call site will hit the method | 50 // we don't need to add a stub. The call site will hit the method |
| 52 // directly. | 51 // directly. |
| 53 return null; | 52 return null; |
| 54 } | 53 } |
| 55 JavaScriptConstantCompiler handler = backend.constants; | 54 JavaScriptConstantCompiler handler = backend.constants; |
| 56 List<String> names = callStructure.getOrderedNamedArguments(); | 55 List<String> names = selector.getOrderedNamedArguments(); |
| 57 | 56 |
| 58 bool isInterceptedMethod = backend.isInterceptedMethod(member); | 57 bool isInterceptedMethod = backend.isInterceptedMethod(member); |
| 59 | 58 |
| 60 // If the method is intercepted, we need to also pass the actual receiver. | 59 // If the method is intercepted, we need to also pass the actual receiver. |
| 61 int extraArgumentCount = isInterceptedMethod ? 1 : 0; | 60 int extraArgumentCount = isInterceptedMethod ? 1 : 0; |
| 62 // Use '$receiver' to avoid clashes with other parameter names. Using | 61 // Use '$receiver' to avoid clashes with other parameter names. Using |
| 63 // '$receiver' works because namer.safeVariableName used for getting paramet
er | 62 // '$receiver' works because namer.safeVariableName used for getting paramet
er |
| 64 // names never returns a name beginning with a single '$'. | 63 // names never returns a name beginning with a single '$'. |
| 65 String receiverArgumentName = r'$receiver'; | 64 String receiverArgumentName = r'$receiver'; |
| 66 | 65 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 // (3) foo$3$d(a, b, d) => foo$4$c$d(a, b, null, d); | 179 // (3) foo$3$d(a, b, d) => foo$4$c$d(a, b, null, d); |
| 181 // (4) No stub generated, call is direct. | 180 // (4) No stub generated, call is direct. |
| 182 // (5) No stub generated, call is direct. | 181 // (5) No stub generated, call is direct. |
| 183 // | 182 // |
| 184 // We need to pay attention if this stub is for a function that has been | 183 // We need to pay attention if this stub is for a function that has been |
| 185 // invoked from a subclass. Then we cannot just redirect, since that | 184 // invoked from a subclass. Then we cannot just redirect, since that |
| 186 // would invoke the methods of the subclass. We have to compile to: | 185 // would invoke the methods of the subclass. We have to compile to: |
| 187 // (1) foo$2(a, b) => MyClass.foo$4$c$d.call(this, a, b, null, null) | 186 // (1) foo$2(a, b) => MyClass.foo$4$c$d.call(this, a, b, null, null) |
| 188 // (2) foo$3$c(a, b, c) => MyClass.foo$4$c$d(this, a, b, c, null); | 187 // (2) foo$3$c(a, b, c) => MyClass.foo$4$c$d(this, a, b, c, null); |
| 189 // (3) foo$3$d(a, b, d) => MyClass.foo$4$c$d(this, a, b, null, d); | 188 // (3) foo$3$d(a, b, d) => MyClass.foo$4$c$d(this, a, b, null, d); |
| 190 List<ParameterStubMethod> generateParameterStubs(MethodElement member, | 189 List<ParameterStubMethod> generateParameterStubs(FunctionElement member, |
| 191 {bool canTearOff: true}) { | 190 {bool canTearOff: true}) { |
| 192 if (member.enclosingElement.isClosure) { | 191 if (member.enclosingElement.isClosure) { |
| 193 ClosureClassElement cls = member.enclosingElement; | 192 ClosureClassElement cls = member.enclosingElement; |
| 194 if (cls.supertype.element == backend.boundClosureClass) { | 193 if (cls.supertype.element == backend.boundClosureClass) { |
| 195 compiler.internalError(cls.methodElement, 'Bound closure1.'); | 194 compiler.internalError(cls.methodElement, 'Bound closure1.'); |
| 196 } | 195 } |
| 197 if (cls.methodElement.isInstanceMember) { | 196 if (cls.methodElement.isInstanceMember) { |
| 198 compiler.internalError(cls.methodElement, 'Bound closure2.'); | 197 compiler.internalError(cls.methodElement, 'Bound closure2.'); |
| 199 } | 198 } |
| 200 } | 199 } |
| 201 | 200 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 // For example, for the call-selector `call(x, y)` the renamed selector | 236 // For example, for the call-selector `call(x, y)` the renamed selector |
| 238 // for member `foo` would be `foo(x, y)`. | 237 // for member `foo` would be `foo(x, y)`. |
| 239 Set<Selector> renamedCallSelectors = | 238 Set<Selector> renamedCallSelectors = |
| 240 callSelectors.isEmpty ? emptySelectorSet : new Set<Selector>(); | 239 callSelectors.isEmpty ? emptySelectorSet : new Set<Selector>(); |
| 241 | 240 |
| 242 Set<Selector> untypedSelectors = new Set<Selector>(); | 241 Set<Selector> untypedSelectors = new Set<Selector>(); |
| 243 | 242 |
| 244 // Start with the callSelectors since they imply the generation of the | 243 // Start with the callSelectors since they imply the generation of the |
| 245 // non-call version. | 244 // non-call version. |
| 246 for (Selector selector in callSelectors) { | 245 for (Selector selector in callSelectors) { |
| 247 Selector renamedSelector = new Selector( | 246 Selector renamedSelector = new Selector.call( |
| 248 SelectorKind.CALL, | 247 member.name, member.library, |
| 249 member.memberName, | 248 selector.argumentCount, selector.namedArguments); |
| 250 selector.callStructure); | |
| 251 renamedCallSelectors.add(renamedSelector); | 249 renamedCallSelectors.add(renamedSelector); |
| 252 | 250 |
| 253 if (!renamedSelector.appliesUnnamed(member, compiler.world)) continue; | 251 if (!renamedSelector.appliesUnnamed(member, compiler.world)) continue; |
| 254 | 252 |
| 255 if (untypedSelectors.add(renamedSelector.asUntyped)) { | 253 if (untypedSelectors.add(renamedSelector.asUntyped)) { |
| 256 ParameterStubMethod stub = | 254 ParameterStubMethod stub = |
| 257 generateParameterStub(member, renamedSelector, selector); | 255 generateParameterStub(member, renamedSelector, selector); |
| 258 if (stub != null) { | 256 if (stub != null) { |
| 259 stubs.add(stub); | 257 stubs.add(stub); |
| 260 } | 258 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 273 generateParameterStub(member, selector, null); | 271 generateParameterStub(member, selector, null); |
| 274 if (stub != null) { | 272 if (stub != null) { |
| 275 stubs.add(stub); | 273 stubs.add(stub); |
| 276 } | 274 } |
| 277 } | 275 } |
| 278 } | 276 } |
| 279 | 277 |
| 280 return stubs; | 278 return stubs; |
| 281 } | 279 } |
| 282 } | 280 } |
| OLD | NEW |