Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Unified Diff: pkg/compiler/lib/src/js_backend/no_such_method_registry.dart

Issue 996263002: Don't generate forwarding hooks if all noSuchMethod implementations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Return enum result for _categorizeImpl Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/js_backend/no_such_method_registry.dart
diff --git a/pkg/compiler/lib/src/js_backend/no_such_method_registry.dart b/pkg/compiler/lib/src/js_backend/no_such_method_registry.dart
new file mode 100644
index 0000000000000000000000000000000000000000..dac590560768a061f74414fe6cc77fe959313211
--- /dev/null
+++ b/pkg/compiler/lib/src/js_backend/no_such_method_registry.dart
@@ -0,0 +1,151 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of js_backend;
+
+/**
+ * Categorizes `noSuchMethod` implementations.
+ *
+ * If user code includes `noSuchMethod` implementations, type inference is
+ * hindered because (for instance) any selector where the type of the
+ * receiver is not known all implementations of `noSuchMethod` must be taken
+ * into account when inferring the return type.
+ *
+ * The situation can be ameliorated with some heuristics for disregarding some
+ * `noSuchMethod` implementations during type inference. We can partition
+ * `noSuchMethod` implementations into 3 categories.
+ *
+ * Implementations in category A have the form
+ *
+ * noSuchMethod(x) => super.noSuchMethod(x);
+ *
+ * where the `super.noSuchMethod` hits `Object.noSuchMethod`,
+ * `Interceptor.noSuchMethod`, or another implementation in category A.
+ *
+ * Implementations in category B immediately throw, and all other
+ * implementations are in category C. A and B implementations may be safely
+ * ignored during type inference. After type inference, we may further partition
+ * C into C1 and C2, where C1 implementations have no return type (they are
+ * guaranteed to throw) and C2, those with return types.
+ */
+class NoSuchMethodRegistry {
+ /// The implementations that fall into category A, described above.
+ final Set<FunctionElement> forwardingImpls = new Set<FunctionElement>();
+ /// The implementations that fall into category B, described above.
+ final Set<FunctionElement> throwingImpls = new Set<FunctionElement>();
+ /// The implementations that fall into category C, described above.
+ final Set<Element> otherImpls = new Set<Element>();
+
+ /// The implementations that have not yet been categorized.
+ final Set<Element> uncategorizedImpls = new Set<Element>();
+
+ final JavaScriptBackend backend;
+ final Compiler compiler;
+
+ NoSuchMethodRegistry(JavaScriptBackend backend)
+ : this.backend = backend,
+ this.compiler = backend.compiler;
+
+ void registerNoSuchMethod(Element noSuchMethodElement) {
+ uncategorizedImpls.add(noSuchMethodElement);
+ }
+
+ void onQueueEmpty(Enqueuer enqueuer) {
+ uncategorizedImpls.forEach(_categorizeImpl);
+ uncategorizedImpls.clear();
+ }
+
+ NsmCategory _categorizeImpl(Element noSuchMethodElement) {
+ assert(noSuchMethodElement.name == Compiler.NO_SUCH_METHOD);
+ if (forwardingImpls.contains(noSuchMethodElement)) {
+ return NsmCategory.FORWARDING;
+ }
+ if (throwingImpls.contains(noSuchMethodElement)) {
+ return NsmCategory.THROWING;
+ }
+ if (otherImpls.contains(noSuchMethodElement)) {
+ return NsmCategory.OTHER;
+ }
+ if (noSuchMethodElement is! FunctionElement ||
+ !compiler.noSuchMethodSelector.signatureApplies(noSuchMethodElement)) {
+ otherImpls.add(noSuchMethodElement);
+ return NsmCategory.OTHER;
+ }
+ FunctionElement noSuchMethodFunc = noSuchMethodElement as FunctionElement;
+ if (isForwarding(noSuchMethodFunc)) {
+ forwardingImpls.add(noSuchMethodFunc);
+ return NsmCategory.FORWARDING;
+ } else if (isThrowing(noSuchMethodFunc)) {
+ throwingImpls.add(noSuchMethodFunc);
+ return NsmCategory.THROWING;
+ } else {
+ otherImpls.add(noSuchMethodFunc);
+ return NsmCategory.OTHER;
+ }
+ }
+
+ bool isForwarding(FunctionElement element) {
+ if (backend.isDefaultNoSuchMethodImplementation(element)) {
+ return true;
+ }
+
+ if (!hasForwardingSyntax(element)) {
+ return false;
+ }
+
+ // Check if the super call hits a forwarding implementation
+ Element superCall = element.enclosingClass
+ .lookupSuperSelector(compiler.noSuchMethodSelector);
+ var category = _categorizeImpl(superCall);
Johnni Winther 2015/03/18 10:47:16 Nit: Replace 'var' with 'NsmCategory'. We only use
Harry Terkelsen 2015/03/18 20:54:50 Done.
+ return category == NsmCategory.FORWARDING;
+ }
+
+ bool hasForwardingSyntax(FunctionElement element) {
+ String param = element.parameters.single.name;
+ Statement body = element.node.body;
+ Expression expr;
+ if (body is Return && body.isArrowBody) {
+ expr = body.expression;
+ } else if (body is Block &&
+ !body.statements.isEmpty &&
+ body.statements.nodes.tail.isEmpty) {
+ Statement stmt = body.statements.nodes.head;
+ if (stmt is Return && stmt.hasExpression) {
+ expr = stmt.expression;
+ }
+ }
+ if (expr is Send &&
+ expr.isSuperCall &&
+ expr.selector is Identifier &&
+ (expr.selector as Identifier).source == Compiler.NO_SUCH_METHOD) {
+ var arg = expr.arguments.head;
+ if (arg is Send &&
+ arg.argumentsNode == null &&
+ arg.receiver == null &&
+ arg.selector is Identifier &&
+ arg.selector.source == param) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ bool isThrowing(FunctionElement element) {
+ Statement body = element.node.body;
+ if (body is Return && body.isArrowBody) {
+ if (body.expression is Throw) {
+ return true;
+ }
+ } else if (body is Block &&
+ !body.statements.isEmpty &&
+ body.statements.nodes.tail.isEmpty) {
+ if (body.statements.nodes.head is Throw) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
+
+enum NsmCategory { FORWARDING, THROWING, OTHER }

Powered by Google App Engine
This is Rietveld 408576698