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 interface TreeElements { | 5 interface TreeElements { |
6 Element operator[](Node node); | 6 Element operator[](Node node); |
7 Selector getSelector(Send send); | 7 Selector getSelector(Send send); |
8 DartType getType(TypeAnnotation annotation); | 8 DartType getType(TypeAnnotation annotation); |
9 bool isParameterChecked(Element element); | 9 bool isParameterChecked(Element element); |
10 } | 10 } |
(...skipping 1119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1130 visitIn(node.statements, new BlockScope(scope)); | 1130 visitIn(node.statements, new BlockScope(scope)); |
1131 } | 1131 } |
1132 | 1132 |
1133 visitDoWhile(DoWhile node) { | 1133 visitDoWhile(DoWhile node) { |
1134 visitLoopBodyIn(node, node.body, new BlockScope(scope)); | 1134 visitLoopBodyIn(node, node.body, new BlockScope(scope)); |
1135 visit(node.condition); | 1135 visit(node.condition); |
1136 } | 1136 } |
1137 | 1137 |
1138 visitEmptyStatement(EmptyStatement node) { } | 1138 visitEmptyStatement(EmptyStatement node) { } |
1139 | 1139 |
1140 bool isAssertSyntax(Expression expression) { | |
ngeoffray
2012/09/05 10:37:10
I'm not sure about this code. I wish we could avoi
Lasse Reichstein Nielsen
2012/09/05 12:51:20
I've moved this into visitSend now, so it only dec
| |
1141 Send send = expression.asSend(); | |
1142 if (send === null) return false; | |
1143 if (send.receiver !== null) return false; | |
1144 if (!send.isCall) return false; | |
1145 Identifier selector = send.selector.asIdentifier(); | |
1146 if (selector === null) return false; | |
1147 if (selector.source.stringValue !== "assert") return false; | |
1148 if (send.argumentCount() != 1) return false; | |
1149 return true; | |
1150 } | |
1151 | |
1140 visitExpressionStatement(ExpressionStatement node) { | 1152 visitExpressionStatement(ExpressionStatement node) { |
1153 if (isAssertSyntax(node.expression)) { | |
ahe
2012/09/05 11:09:54
I'm going to express myself unambiguously. I do no
Lasse Reichstein Nielsen
2012/09/05 12:51:20
Try seeing if the new version is better.
| |
1154 Send send = node.expression; | |
1155 Element scopedAssert = scope.lexicalLookup(const SourceString("assert")); | |
1156 if (scopedAssert == null) { | |
1157 // Compile as a call to a top-level function. | |
1158 Element target = compiler.assertMethod; | |
1159 Selector selector = resolveSelector(send); | |
1160 resolveArguments(send.argumentsNode); | |
1161 useElement(send, target); | |
1162 registerSend(selector, target); | |
1163 return; | |
1164 } | |
1165 } | |
1141 visit(node.expression); | 1166 visit(node.expression); |
1142 } | 1167 } |
1143 | 1168 |
1144 visitFor(For node) { | 1169 visitFor(For node) { |
1145 Scope blockScope = new BlockScope(scope); | 1170 Scope blockScope = new BlockScope(scope); |
1146 visitIn(node.initializer, blockScope); | 1171 visitIn(node.initializer, blockScope); |
1147 visitIn(node.condition, blockScope); | 1172 visitIn(node.condition, blockScope); |
1148 visitIn(node.update, blockScope); | 1173 visitIn(node.update, blockScope); |
1149 visitLoopBodyIn(node, node.body, blockScope); | 1174 visitLoopBodyIn(node, node.body, blockScope); |
1150 } | 1175 } |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1372 // we need to register that fact that we may be calling a closure | 1397 // we need to register that fact that we may be calling a closure |
1373 // with the same arguments. | 1398 // with the same arguments. |
1374 if (node.isCall && | 1399 if (node.isCall && |
1375 (Element.isInvalid(target) || | 1400 (Element.isInvalid(target) || |
1376 target.isGetter() || | 1401 target.isGetter() || |
1377 Elements.isClosureSend(node, target))) { | 1402 Elements.isClosureSend(node, target))) { |
1378 Selector call = new Selector.callClosureFrom(selector); | 1403 Selector call = new Selector.callClosureFrom(selector); |
1379 world.registerDynamicInvocation(call.name, call); | 1404 world.registerDynamicInvocation(call.name, call); |
1380 } | 1405 } |
1381 | 1406 |
1382 // TODO(ngeoffray): We should do the check in | |
1383 // visitExpressionStatement instead. | |
1384 if (target === compiler.assertMethod && !node.isCall) { | |
1385 // We can only use assert by calling it. | |
1386 if (!inInstanceContext) { | |
1387 error(node, MessageKind.MISSING_ARGUMENTS_TO_ASSERT, [node]); | |
1388 } | |
1389 target = null; | |
1390 } | |
1391 | |
1392 // TODO(ngeoffray): Warn if target is null and the send is | 1407 // TODO(ngeoffray): Warn if target is null and the send is |
1393 // unqualified. | 1408 // unqualified. |
1394 useElement(node, target); | 1409 useElement(node, target); |
1395 registerSend(selector, target); | 1410 registerSend(selector, target); |
1396 return node.isPropertyAccess ? target : null; | 1411 return node.isPropertyAccess ? target : null; |
1397 } | 1412 } |
1398 | 1413 |
1399 visitSendSet(SendSet node) { | 1414 visitSendSet(SendSet node) { |
1400 Element target = resolveSend(node); | 1415 Element target = resolveSend(node); |
1401 Element setter = target; | 1416 Element setter = target; |
(...skipping 1148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2550 return e; | 2565 return e; |
2551 } | 2566 } |
2552 } | 2567 } |
2553 | 2568 |
2554 class Scope { | 2569 class Scope { |
2555 final Element element; | 2570 final Element element; |
2556 final Scope parent; | 2571 final Scope parent; |
2557 | 2572 |
2558 Scope(this.parent, this.element); | 2573 Scope(this.parent, this.element); |
2559 abstract Element add(Element element); | 2574 abstract Element add(Element element); |
2560 abstract Element lookup(SourceString name); | 2575 |
2576 Element lookup(SourceString name) { | |
2577 Element result = localLookup(name); | |
2578 if (result != null) return result; | |
2579 return parent.lookup(name); | |
2580 } | |
2581 | |
2582 Element lexicalLookup(SourceString name) { | |
2583 Element result = localLookup(name); | |
2584 if (result != null) return result; | |
2585 return parent.lexicalLookup(name); | |
2586 } | |
2587 | |
2588 abstract Element localLookup(SourceString name); | |
2561 } | 2589 } |
2562 | 2590 |
2563 /** | 2591 /** |
2564 * [TypeDeclarationScope] defines the outer scope of a type declaration in | 2592 * [TypeDeclarationScope] defines the outer scope of a type declaration in |
2565 * which the declared type variables and the entities in the enclosing scope are | 2593 * which the declared type variables and the entities in the enclosing scope are |
2566 * available but where declared and inherited members are not available. This | 2594 * available but where declared and inherited members are not available. This |
2567 * scope is only used for class/interface declarations during resolution of the | 2595 * scope is only used for class/interface declarations during resolution of the |
2568 * class hierarchy. In all other cases [ClassScope] is used. | 2596 * class hierarchy. In all other cases [ClassScope] is used. |
2569 */ | 2597 */ |
2570 class TypeDeclarationScope extends Scope { | 2598 class TypeDeclarationScope extends Scope { |
2571 TypeDeclarationElement get element => super.element; | 2599 TypeDeclarationElement get element => super.element; |
2572 | 2600 |
2573 TypeDeclarationScope(parent, TypeDeclarationElement element) | 2601 TypeDeclarationScope(parent, TypeDeclarationElement element) |
2574 : super(parent, element) { | 2602 : super(parent, element) { |
2575 assert(parent !== null); | 2603 assert(parent !== null); |
2576 } | 2604 } |
2577 | 2605 |
2578 Element add(Element newElement) { | 2606 Element add(Element newElement) { |
2579 throw "Cannot add element to TypeDeclarationScope"; | 2607 throw "Cannot add element to TypeDeclarationScope"; |
2580 } | 2608 } |
2581 | 2609 |
2582 /** | 2610 /** |
2583 * Looks up [name] within the type variables declared in [element]. | 2611 * Looks up [name] within the type variables declared in [element]. |
2584 */ | 2612 */ |
2585 Element lookupTypeVariable(SourceString name) { | 2613 Element lookupTypeVariable(SourceString name) { |
2586 return null; | 2614 return null; |
2587 } | 2615 } |
2588 | 2616 |
2589 Element lookup(SourceString name) { | 2617 Element localLookup(SourceString name) { |
2590 Link<DartType> typeVariableLink = element.typeVariables; | 2618 Link<DartType> typeVariableLink = element.typeVariables; |
2591 while (!typeVariableLink.isEmpty()) { | 2619 while (!typeVariableLink.isEmpty()) { |
2592 TypeVariableType typeVariable = typeVariableLink.head; | 2620 TypeVariableType typeVariable = typeVariableLink.head; |
2593 if (typeVariable.name == name) { | 2621 if (typeVariable.name == name) { |
2594 return typeVariable.element; | 2622 return typeVariable.element; |
2595 } | 2623 } |
2596 typeVariableLink = typeVariableLink.tail; | 2624 typeVariableLink = typeVariableLink.tail; |
2597 } | 2625 } |
2598 | 2626 return null; |
2599 return parent.lookup(name); | |
2600 } | 2627 } |
2601 | 2628 |
2602 String toString() => | 2629 String toString() => |
2603 '$element${element.typeVariables} > $parent'; | 2630 '$element${element.typeVariables} > $parent'; |
2604 } | 2631 } |
2605 | 2632 |
2606 class MethodScope extends Scope { | 2633 class MethodScope extends Scope { |
2607 final Map<SourceString, Element> elements; | 2634 final Map<SourceString, Element> elements; |
2608 | 2635 |
2609 MethodScope(Scope parent, Element element) | 2636 MethodScope(Scope parent, Element element) |
2610 : super(parent, element), | 2637 : super(parent, element), |
2611 this.elements = new Map<SourceString, Element>() { | 2638 this.elements = new Map<SourceString, Element>() { |
2612 assert(parent !== null); | 2639 assert(parent !== null); |
2613 } | 2640 } |
2614 | 2641 |
2615 Element lookup(SourceString name) { | 2642 Element localLookup(SourceString name) => elements[name]; |
2616 Element found = elements[name]; | |
2617 if (found !== null) return found; | |
2618 return parent.lookup(name); | |
2619 } | |
2620 | 2643 |
2621 Element add(Element newElement) { | 2644 Element add(Element newElement) { |
2622 if (elements.containsKey(newElement.name)) { | 2645 if (elements.containsKey(newElement.name)) { |
2623 return elements[newElement.name]; | 2646 return elements[newElement.name]; |
2624 } | 2647 } |
2625 elements[newElement.name] = newElement; | 2648 elements[newElement.name] = newElement; |
2626 return newElement; | 2649 return newElement; |
2627 } | 2650 } |
2628 | 2651 |
2629 String toString() => '$element${elements.getKeys()} > $parent'; | 2652 String toString() => '$element${elements.getKeys()} > $parent'; |
2630 } | 2653 } |
2631 | 2654 |
2632 class BlockScope extends MethodScope { | 2655 class BlockScope extends MethodScope { |
2633 BlockScope(Scope parent) : super(parent, parent.element); | 2656 BlockScope(Scope parent) : super(parent, parent.element); |
2634 | 2657 |
2635 String toString() => 'block${elements.getKeys()} > $parent'; | 2658 String toString() => 'block${elements.getKeys()} > $parent'; |
2636 } | 2659 } |
2637 | 2660 |
2638 /** | 2661 /** |
2639 * [ClassScope] defines the inner scope of a class/interface declaration in | 2662 * [ClassScope] defines the inner scope of a class/interface declaration in |
2640 * which declared members, declared type variables, entities in the enclosing | 2663 * which declared members, declared type variables, entities in the enclosing |
2641 * scope and inherited members are available, in the given order. | 2664 * scope and inherited members are available, in the given order. |
2642 */ | 2665 */ |
2643 class ClassScope extends TypeDeclarationScope { | 2666 class ClassScope extends TypeDeclarationScope { |
2644 ClassScope(Scope parentScope, ClassElement element) | 2667 ClassScope(Scope parentScope, ClassElement element) |
2645 : super(parentScope, element); | 2668 : super(parentScope, element); |
2646 | 2669 |
2647 Element lookup(SourceString name) { | 2670 Element localLookup(SourceString name) { |
2648 ClassElement cls = element; | 2671 ClassElement cls = element; |
2649 Element result = cls.lookupLocalMember(name); | 2672 Element result = cls.lookupLocalMember(name); |
2650 if (result !== null) return result; | 2673 if (result !== null) return result; |
2651 result = super.lookup(name); | 2674 return super.localLookup(name); |
2652 if (result != null) return result; | 2675 } |
2676 | |
2677 Element lookup(SourceString name) { | |
2678 Element result = localLookup(name); | |
2679 if (result !== null) return result; | |
2680 result = parent.lexicalLookup(name); | |
2681 if (result !== null) return result; | |
2682 ClassElement cls = element; | |
2653 return cls.lookupSuperMember(name); | 2683 return cls.lookupSuperMember(name); |
2654 } | 2684 } |
2655 | 2685 |
2656 Element add(Element newElement) { | 2686 Element add(Element newElement) { |
2657 throw "Cannot add an element in a class scope"; | 2687 throw "Cannot add an element in a class scope"; |
2658 } | 2688 } |
2659 | 2689 |
2660 String toString() => '$element > $parent'; | 2690 String toString() => '$element > $parent'; |
2661 } | 2691 } |
2662 | 2692 |
2663 class TopScope extends Scope { | 2693 class TopScope extends Scope { |
2664 LibraryElement get library => element; | 2694 LibraryElement get library => element; |
2665 | 2695 |
2666 TopScope(LibraryElement library) : super(null, library); | 2696 TopScope(LibraryElement library) : super(null, library); |
2667 Element lookup(SourceString name) { | 2697 |
2668 return library.find(name); | 2698 Element localLookup(SourceString name) => library.find(name); |
2669 } | 2699 Element lookup(SourceString name) => localLookup(name); |
2700 Element lexicalLookup(SourceString name) => localLookup(name); | |
2670 | 2701 |
2671 Element add(Element newElement) { | 2702 Element add(Element newElement) { |
2672 throw "Cannot add an element in the top scope"; | 2703 throw "Cannot add an element in the top scope"; |
2673 } | 2704 } |
2674 String toString() => '$element'; | 2705 String toString() => '$element'; |
2675 } | 2706 } |
OLD | NEW |