| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 dart_backend; | 5 part of dart_backend; |
| 6 | 6 |
| 7 class LocalPlaceholder { | 7 class LocalPlaceholder { |
| 8 final String identifier; | 8 final String identifier; |
| 9 final Set<Node> nodes; | 9 final Set<Node> nodes; |
| 10 LocalPlaceholder(this.identifier) : nodes = new Set<Node>(); | 10 LocalPlaceholder(this.identifier) : nodes = new Set<Node>(); |
| 11 int get hashCode => identifier.hashCode; | 11 int get hashCode => identifier.hashCode; |
| 12 String toString() => | 12 String toString() => |
| 13 'local_placeholder[id($identifier), nodes($nodes)]'; | 13 'local_placeholder[id($identifier), nodes($nodes)]'; |
| 14 } | 14 } |
| 15 | 15 |
| 16 class FunctionScope { | 16 class FunctionScope { |
| 17 final Set<String> parameterIdentifiers; | 17 final Set<String> parameterIdentifiers; |
| 18 final Set<LocalPlaceholder> localPlaceholders; | 18 final Set<LocalPlaceholder> localPlaceholders; |
| 19 FunctionScope() | 19 FunctionScope() |
| 20 : parameterIdentifiers = new Set<String>(), | 20 : parameterIdentifiers = new Set<String>(), |
| 21 localPlaceholders = new Set<LocalPlaceholder>(); | 21 localPlaceholders = new Set<LocalPlaceholder>(); |
| 22 void registerParameter(Identifier node) { | 22 void registerParameter(Identifier node) { |
| 23 parameterIdentifiers.add(node.source.slowToString()); | 23 parameterIdentifiers.add(node.source.slowToString()); |
| 24 } | 24 } |
| 25 } | 25 } |
| 26 | 26 |
| 27 class ConstructorPlaceholder { | 27 class ConstructorPlaceholder { |
| 28 final Node node; | 28 final Node node; |
| 29 final DartType type; | 29 final DartType type; |
| 30 ConstructorPlaceholder(this.node, this.type); | 30 final bool isRedirectingCall; |
| 31 ConstructorPlaceholder(this.node, this.type) |
| 32 : this.isRedirectingCall = false; |
| 33 ConstructorPlaceholder.redirectingCall(this.node) |
| 34 : this.type = null, this.isRedirectingCall = true; |
| 31 } | 35 } |
| 32 | 36 |
| 33 class DeclarationTypePlaceholder { | 37 class DeclarationTypePlaceholder { |
| 34 final TypeAnnotation typeNode; | 38 final TypeAnnotation typeNode; |
| 35 final bool requiresVar; | 39 final bool requiresVar; |
| 36 DeclarationTypePlaceholder(this.typeNode, this.requiresVar); | 40 DeclarationTypePlaceholder(this.typeNode, this.requiresVar); |
| 37 } | 41 } |
| 38 | 42 |
| 39 class SendVisitor extends ResolvedVisitor { | 43 class SendVisitor extends ResolvedVisitor { |
| 40 final PlaceholderCollector collector; | 44 final PlaceholderCollector collector; |
| 41 | 45 |
| 42 get compiler => collector.compiler; | 46 get compiler => collector.compiler; |
| 43 | 47 |
| 44 SendVisitor(this.collector, TreeElements elements) : super(elements); | 48 SendVisitor(this.collector, TreeElements elements) : super(elements); |
| 45 | 49 |
| 46 visitOperatorSend(Send node) {} | 50 visitOperatorSend(Send node) {} |
| 47 visitForeignSend(Send node) {} | 51 visitForeignSend(Send node) {} |
| 48 | 52 |
| 49 visitSuperSend(Send node) { | 53 visitSuperSend(Send node) { |
| 50 collector.tryMakeMemberPlaceholder(node.selector); | 54 Element element = elements[node]; |
| 55 if (element != null && element.isConstructor()) { |
| 56 collector.makeRedirectingConstructorPlaceholder(node.selector, element); |
| 57 } else { |
| 58 collector.tryMakeMemberPlaceholder(node.selector); |
| 59 } |
| 51 } | 60 } |
| 52 | 61 |
| 53 visitDynamicSend(Send node) { | 62 visitDynamicSend(Send node) { |
| 54 final element = elements[node]; | 63 final element = elements[node]; |
| 55 if (element == null || !element.isErroneous()) { | 64 if (element == null || !element.isErroneous()) { |
| 56 collector.tryMakeMemberPlaceholder(node.selector); | 65 collector.tryMakeMemberPlaceholder(node.selector); |
| 57 } | 66 } |
| 58 } | 67 } |
| 59 | 68 |
| 60 visitClosureSend(Send node) { | 69 visitClosureSend(Send node) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 91 } | 100 } |
| 92 } | 101 } |
| 93 } | 102 } |
| 94 | 103 |
| 95 visitStaticSend(Send node) { | 104 visitStaticSend(Send node) { |
| 96 final element = elements[node]; | 105 final element = elements[node]; |
| 97 if (Elements.isUnresolved(element) | 106 if (Elements.isUnresolved(element) |
| 98 || identical(element, compiler.assertMethod)) { | 107 || identical(element, compiler.assertMethod)) { |
| 99 return; | 108 return; |
| 100 } | 109 } |
| 101 // TODO(smok): We should never go inside this IF, check? | |
| 102 if (element.isConstructor() || element.isFactoryConstructor()) { | 110 if (element.isConstructor() || element.isFactoryConstructor()) { |
| 103 // Rename named constructor in redirection position: | 111 // Rename named constructor in redirection position: |
| 104 // class C { C.named(); C.redirecting() : this.named(); } | 112 // class C { C.named(); C.redirecting() : this.named(); } |
| 105 // TODO(smok): Fix redirecting constructors. | |
| 106 if (node.receiver is Identifier | 113 if (node.receiver is Identifier |
| 107 && node.receiver.asIdentifier().isThis()) { | 114 && node.receiver.asIdentifier().isThis()) { |
| 108 assert(node.selector is Identifier); | 115 assert(node.selector is Identifier); |
| 109 collector.tryMakeMemberPlaceholder(node.selector); | 116 collector.makeRedirectingConstructorPlaceholder(node.selector, element); |
| 110 } | 117 } |
| 111 return; | 118 return; |
| 112 } | 119 } |
| 113 collector.makeElementPlaceholder(node.selector, element); | 120 collector.makeElementPlaceholder(node.selector, element); |
| 114 // Another ugly case: <lib prefix>.<top level> is represented as | 121 // Another ugly case: <lib prefix>.<top level> is represented as |
| 115 // receiver: lib prefix, selector: top level. | 122 // receiver: lib prefix, selector: top level. |
| 116 if (element.isTopLevel() && node.receiver != null) { | 123 if (element.isTopLevel() && node.receiver != null) { |
| 117 assert(elements[node.receiver].isPrefix()); | 124 assert(elements[node.receiver].isPrefix()); |
| 118 // Hack: putting null into map overrides receiver of original node. | 125 // Hack: putting null into map overrides receiver of original node. |
| 119 collector.makeNullPlaceholder(node.receiver); | 126 collector.makeNullPlaceholder(node.receiver); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 nullNodes = new Set<Node>(), | 159 nullNodes = new Set<Node>(), |
| 153 unresolvedNodes = new Set<Identifier>(), | 160 unresolvedNodes = new Set<Identifier>(), |
| 154 elementNodes = new Map<Element, Set<Node>>(), | 161 elementNodes = new Map<Element, Set<Node>>(), |
| 155 functionScopes = new Map<FunctionElement, FunctionScope>(), | 162 functionScopes = new Map<FunctionElement, FunctionScope>(), |
| 156 privateNodes = new Map<LibraryElement, Set<Identifier>>(), | 163 privateNodes = new Map<LibraryElement, Set<Identifier>>(), |
| 157 declarationTypePlaceholders = new List<DeclarationTypePlaceholder>(), | 164 declarationTypePlaceholders = new List<DeclarationTypePlaceholder>(), |
| 158 memberPlaceholders = new Map<String, Set<Identifier>>(), | 165 memberPlaceholders = new Map<String, Set<Identifier>>(), |
| 159 constructorPlaceholders = | 166 constructorPlaceholders = |
| 160 new Map<Element, List<ConstructorPlaceholder>>(); | 167 new Map<Element, List<ConstructorPlaceholder>>(); |
| 161 | 168 |
| 162 void tryMakeConstructorPlaceholder( | |
| 163 FunctionExpression constructor, FunctionElement constructorElement) { | |
| 164 DartType type = constructorElement.getEnclosingClass().type.asRaw(); | |
| 165 makeConstructorPlaceholder(constructor.name, constructorElement, type); | |
| 166 } | |
| 167 | |
| 168 void collectFunctionDeclarationPlaceholders( | 169 void collectFunctionDeclarationPlaceholders( |
| 169 FunctionElement element, FunctionExpression node) { | 170 FunctionElement element, FunctionExpression node) { |
| 170 if (element.isGenerativeConstructor() || element.isFactoryConstructor()) { | 171 if (element.isGenerativeConstructor() || element.isFactoryConstructor()) { |
| 171 // Two complicated cases for class/interface renaming: | 172 DartType type = element.getEnclosingClass().type.asRaw(); |
| 172 // 1) class which implements constructors of other interfaces, but not | 173 makeConstructorPlaceholder(node.name, element, type); |
| 173 // implements interfaces themselves: | |
| 174 // 0.dart: class C { I(); } | |
| 175 // 1.dart and 2.dart: interface I default C { I(); } | |
| 176 // now we have to duplicate our I() constructor in C class with | |
| 177 // proper names. | |
| 178 // 2) (even worse for us): | |
| 179 // 0.dart: class C { C(); } | |
| 180 // 1.dart: interface C default p0.C { C(); } | |
| 181 // the second case is just a bug now. | |
| 182 tryMakeConstructorPlaceholder(node, element); | |
| 183 } else if (Elements.isStaticOrTopLevel(element)) { | 174 } else if (Elements.isStaticOrTopLevel(element)) { |
| 184 // Note: this code should only rename private identifiers for class' | 175 // Note: this code should only rename private identifiers for class' |
| 185 // fields/getters/setters/methods. Top-level identifiers are renamed | 176 // fields/getters/setters/methods. Top-level identifiers are renamed |
| 186 // just to escape conflicts and that should be enough as we shouldn't | 177 // just to escape conflicts and that should be enough as we shouldn't |
| 187 // be able to resolve private identifiers for other libraries. | 178 // be able to resolve private identifiers for other libraries. |
| 188 makeElementPlaceholder(node.name, element); | 179 makeElementPlaceholder(node.name, element); |
| 189 } else if (element.isMember()) { | 180 } else if (element.isMember()) { |
| 190 if (node.name is Identifier) { | 181 if (node.name is Identifier) { |
| 191 tryMakeMemberPlaceholder(node.name); | 182 tryMakeMemberPlaceholder(node.name); |
| 192 } else { | 183 } else { |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 LocalPlaceholder localPlaceholder = new LocalPlaceholder(name); | 316 LocalPlaceholder localPlaceholder = new LocalPlaceholder(name); |
| 326 currentFunctionScope.localPlaceholders.add(localPlaceholder); | 317 currentFunctionScope.localPlaceholders.add(localPlaceholder); |
| 327 return localPlaceholder; | 318 return localPlaceholder; |
| 328 }); | 319 }); |
| 329 } | 320 } |
| 330 | 321 |
| 331 getLocalPlaceholder().nodes.add(identifier); | 322 getLocalPlaceholder().nodes.add(identifier); |
| 332 } | 323 } |
| 333 | 324 |
| 334 void makeConstructorPlaceholder(Node node, Element element, DartType type) { | 325 void makeConstructorPlaceholder(Node node, Element element, DartType type) { |
| 326 assert(type != null); |
| 335 constructorPlaceholders | 327 constructorPlaceholders |
| 336 .putIfAbsent(element, () => <ConstructorPlaceholder>[]) | 328 .putIfAbsent(element, () => <ConstructorPlaceholder>[]) |
| 337 .add(new ConstructorPlaceholder(node, type)); | 329 .add(new ConstructorPlaceholder(node, type)); |
| 338 } | 330 } |
| 331 void makeRedirectingConstructorPlaceholder(Node node, Element element) { |
| 332 constructorPlaceholders |
| 333 .putIfAbsent(element, () => <ConstructorPlaceholder>[]) |
| 334 .add(new ConstructorPlaceholder.redirectingCall(node)); |
| 335 } |
| 339 | 336 |
| 340 void internalError(String reason, {Node node}) { | 337 void internalError(String reason, {Node node}) { |
| 341 compiler.cancel(reason, node: node); | 338 compiler.cancel(reason, node: node); |
| 342 } | 339 } |
| 343 | 340 |
| 344 void unreachable() { internalError('Unreachable case'); } | 341 void unreachable() { internalError('Unreachable case'); } |
| 345 | 342 |
| 346 visit(Node node) => (node == null) ? null : node.accept(this); | 343 visit(Node node) => (node == null) ? null : node.accept(this); |
| 347 | 344 |
| 348 visitNode(Node node) { node.visitChildren(this); } // We must go deeper. | 345 visitNode(Node node) { node.visitChildren(this); } // We must go deeper. |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 | 600 |
| 604 visitBlock(Block node) { | 601 visitBlock(Block node) { |
| 605 for (Node statement in node.statements.nodes) { | 602 for (Node statement in node.statements.nodes) { |
| 606 if (statement is VariableDefinitions) { | 603 if (statement is VariableDefinitions) { |
| 607 makeVarDeclarationTypePlaceholder(statement); | 604 makeVarDeclarationTypePlaceholder(statement); |
| 608 } | 605 } |
| 609 } | 606 } |
| 610 node.visitChildren(this); | 607 node.visitChildren(this); |
| 611 } | 608 } |
| 612 } | 609 } |
| OLD | NEW |