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

Side by Side Diff: lib/compiler/implementation/resolver.dart

Issue 10915083: Change assert implementation to not depend on a top-level function called 'assert'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Moved _assert to js_helper. Created 8 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 936 matching lines...) Expand 10 before | Expand all | Expand 10 after
947 } 947 }
948 } 948 }
949 949
950 class ResolverVisitor extends CommonResolverVisitor<Element> { 950 class ResolverVisitor extends CommonResolverVisitor<Element> {
951 final TreeElementMapping mapping; 951 final TreeElementMapping mapping;
952 final Element enclosingElement; 952 final Element enclosingElement;
953 final TypeResolver typeResolver; 953 final TypeResolver typeResolver;
954 bool inInstanceContext; 954 bool inInstanceContext;
955 Scope scope; 955 Scope scope;
956 ClassElement currentClass; 956 ClassElement currentClass;
957 ExpressionStatement currentExpressionStatement;
957 bool typeRequired = false; 958 bool typeRequired = false;
958 StatementScope statementScope; 959 StatementScope statementScope;
959 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION; 960 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION;
960 961
961 ResolverVisitor(Compiler compiler, Element element) 962 ResolverVisitor(Compiler compiler, Element element)
962 : this.mapping = new TreeElementMapping(), 963 : this.mapping = new TreeElementMapping(),
963 this.enclosingElement = element, 964 this.enclosingElement = element,
964 // When the element is a field, we are actually resolving its 965 // When the element is a field, we are actually resolving its
965 // initial value, which should not have access to instance 966 // initial value, which should not have access to instance
966 // fields. 967 // fields.
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1131 } 1132 }
1132 1133
1133 visitDoWhile(DoWhile node) { 1134 visitDoWhile(DoWhile node) {
1134 visitLoopBodyIn(node, node.body, new BlockScope(scope)); 1135 visitLoopBodyIn(node, node.body, new BlockScope(scope));
1135 visit(node.condition); 1136 visit(node.condition);
1136 } 1137 }
1137 1138
1138 visitEmptyStatement(EmptyStatement node) { } 1139 visitEmptyStatement(EmptyStatement node) { }
1139 1140
1140 visitExpressionStatement(ExpressionStatement node) { 1141 visitExpressionStatement(ExpressionStatement node) {
1142 ExpressionStatement oldExpressionStatement = currentExpressionStatement;
1143 currentExpressionStatement = node;
1141 visit(node.expression); 1144 visit(node.expression);
1145 currentExpressionStatement = oldExpressionStatement;
1142 } 1146 }
1143 1147
1144 visitFor(For node) { 1148 visitFor(For node) {
1145 Scope blockScope = new BlockScope(scope); 1149 Scope blockScope = new BlockScope(scope);
1146 visitIn(node.initializer, blockScope); 1150 visitIn(node.initializer, blockScope);
1147 visitIn(node.condition, blockScope); 1151 visitIn(node.condition, blockScope);
1148 visitIn(node.update, blockScope); 1152 visitIn(node.update, blockScope);
1149 visitLoopBodyIn(node, node.body, blockScope); 1153 visitLoopBodyIn(node, node.body, blockScope);
1150 } 1154 }
1151 1155
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 Expression argument = link.head; 1327 Expression argument = link.head;
1324 visit(argument); 1328 visit(argument);
1325 if (argument.asNamedArgument() != null) { 1329 if (argument.asNamedArgument() != null) {
1326 seenNamedArgument = true; 1330 seenNamedArgument = true;
1327 } else if (seenNamedArgument) { 1331 } else if (seenNamedArgument) {
1328 error(argument, MessageKind.INVALID_ARGUMENT_AFTER_NAMED); 1332 error(argument, MessageKind.INVALID_ARGUMENT_AFTER_NAMED);
1329 } 1333 }
1330 } 1334 }
1331 } 1335 }
1332 1336
1337 /**
1338 * Checks if a [Send] represents an assertionStatement.
1339 */
1340 bool isAssert(Send send) {
1341 if (currentExpressionStatement === null ||
1342 currentExpressionStatement.expression !== send) return false;
1343 if (send.receiver !== null) return false;
1344 if (!send.isCall) return false;
1345 Identifier selector = send.selector.asIdentifier();
1346 if (selector === null) return false;
1347 if (selector.source.stringValue !== "assert") return false;
ahe 2012/09/05 13:12:24 I would think this should be the first thing to ch
1348 if (send.argumentCount() != 1) return false;
1349 return true;
1350 }
1351
1333 visitSend(Send node) { 1352 visitSend(Send node) {
1353 if (isAssert(node)) {
ahe 2012/09/05 13:12:24 I still think you're fighting the system. Let's ch
1354 Element scopedAssert = scope.lexicalLookup(const SourceString("assert"));
1355 if (scopedAssert == null) {
1356 // Compile as a call to a top-level function.
1357 Element target = compiler.assertMethod;
1358 Selector selector = resolveSelector(node);
1359 resolveArguments(node.argumentsNode);
1360 useElement(node, target);
1361 registerSend(selector, target);
1362 return;
1363 }
1364 }
1365
1334 Element target = resolveSend(node); 1366 Element target = resolveSend(node);
1335 if (!Element.isInvalid(target) 1367 if (!Element.isInvalid(target)
1336 && target.kind == ElementKind.ABSTRACT_FIELD) { 1368 && target.kind == ElementKind.ABSTRACT_FIELD) {
1337 AbstractFieldElement field = target; 1369 AbstractFieldElement field = target;
1338 target = field.getter; 1370 target = field.getter;
1339 if (Element.isInvalid(target) && !inInstanceContext) { 1371 if (Element.isInvalid(target) && !inInstanceContext) {
1340 // TODO(karlklose): make this a runtime error. 1372 // TODO(karlklose): make this a runtime error.
1341 error(node.selector, MessageKind.CANNOT_RESOLVE_GETTER); 1373 error(node.selector, MessageKind.CANNOT_RESOLVE_GETTER);
1342 } 1374 }
1343 } 1375 }
(...skipping 28 matching lines...) Expand all
1372 // we need to register that fact that we may be calling a closure 1404 // we need to register that fact that we may be calling a closure
1373 // with the same arguments. 1405 // with the same arguments.
1374 if (node.isCall && 1406 if (node.isCall &&
1375 (Element.isInvalid(target) || 1407 (Element.isInvalid(target) ||
1376 target.isGetter() || 1408 target.isGetter() ||
1377 Elements.isClosureSend(node, target))) { 1409 Elements.isClosureSend(node, target))) {
1378 Selector call = new Selector.callClosureFrom(selector); 1410 Selector call = new Selector.callClosureFrom(selector);
1379 world.registerDynamicInvocation(call.name, call); 1411 world.registerDynamicInvocation(call.name, call);
1380 } 1412 }
1381 1413
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 1414 // TODO(ngeoffray): Warn if target is null and the send is
1393 // unqualified. 1415 // unqualified.
1394 useElement(node, target); 1416 useElement(node, target);
1395 registerSend(selector, target); 1417 registerSend(selector, target);
1396 return node.isPropertyAccess ? target : null; 1418 return node.isPropertyAccess ? target : null;
1397 } 1419 }
1398 1420
1399 visitSendSet(SendSet node) { 1421 visitSendSet(SendSet node) {
1400 Element target = resolveSend(node); 1422 Element target = resolveSend(node);
1401 Element setter = target; 1423 Element setter = target;
(...skipping 1148 matching lines...) Expand 10 before | Expand all | Expand 10 after
2550 return e; 2572 return e;
2551 } 2573 }
2552 } 2574 }
2553 2575
2554 class Scope { 2576 class Scope {
2555 final Element element; 2577 final Element element;
2556 final Scope parent; 2578 final Scope parent;
2557 2579
2558 Scope(this.parent, this.element); 2580 Scope(this.parent, this.element);
2559 abstract Element add(Element element); 2581 abstract Element add(Element element);
2560 abstract Element lookup(SourceString name); 2582
2583 Element lookup(SourceString name) {
2584 Element result = localLookup(name);
2585 if (result != null) return result;
2586 return parent.lookup(name);
2587 }
2588
2589 Element lexicalLookup(SourceString name) {
2590 Element result = localLookup(name);
2591 if (result != null) return result;
2592 return parent.lexicalLookup(name);
2593 }
2594
2595 abstract Element localLookup(SourceString name);
2561 } 2596 }
2562 2597
2563 /** 2598 /**
2564 * [TypeDeclarationScope] defines the outer scope of a type declaration in 2599 * [TypeDeclarationScope] defines the outer scope of a type declaration in
2565 * which the declared type variables and the entities in the enclosing scope are 2600 * 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 2601 * available but where declared and inherited members are not available. This
2567 * scope is only used for class/interface declarations during resolution of the 2602 * scope is only used for class/interface declarations during resolution of the
2568 * class hierarchy. In all other cases [ClassScope] is used. 2603 * class hierarchy. In all other cases [ClassScope] is used.
2569 */ 2604 */
2570 class TypeDeclarationScope extends Scope { 2605 class TypeDeclarationScope extends Scope {
2571 TypeDeclarationElement get element => super.element; 2606 TypeDeclarationElement get element => super.element;
2572 2607
2573 TypeDeclarationScope(parent, TypeDeclarationElement element) 2608 TypeDeclarationScope(parent, TypeDeclarationElement element)
2574 : super(parent, element) { 2609 : super(parent, element) {
2575 assert(parent !== null); 2610 assert(parent !== null);
2576 } 2611 }
2577 2612
2578 Element add(Element newElement) { 2613 Element add(Element newElement) {
2579 throw "Cannot add element to TypeDeclarationScope"; 2614 throw "Cannot add element to TypeDeclarationScope";
2580 } 2615 }
2581 2616
2582 /** 2617 /**
2583 * Looks up [name] within the type variables declared in [element]. 2618 * Looks up [name] within the type variables declared in [element].
2584 */ 2619 */
2585 Element lookupTypeVariable(SourceString name) { 2620 Element lookupTypeVariable(SourceString name) {
2586 return null; 2621 return null;
2587 } 2622 }
2588 2623
2589 Element lookup(SourceString name) { 2624 Element localLookup(SourceString name) {
2590 Link<DartType> typeVariableLink = element.typeVariables; 2625 Link<DartType> typeVariableLink = element.typeVariables;
2591 while (!typeVariableLink.isEmpty()) { 2626 while (!typeVariableLink.isEmpty()) {
2592 TypeVariableType typeVariable = typeVariableLink.head; 2627 TypeVariableType typeVariable = typeVariableLink.head;
2593 if (typeVariable.name == name) { 2628 if (typeVariable.name == name) {
2594 return typeVariable.element; 2629 return typeVariable.element;
2595 } 2630 }
2596 typeVariableLink = typeVariableLink.tail; 2631 typeVariableLink = typeVariableLink.tail;
2597 } 2632 }
2598 2633 return null;
2599 return parent.lookup(name);
2600 } 2634 }
2601 2635
2602 String toString() => 2636 String toString() =>
2603 '$element${element.typeVariables} > $parent'; 2637 '$element${element.typeVariables} > $parent';
2604 } 2638 }
2605 2639
2606 class MethodScope extends Scope { 2640 class MethodScope extends Scope {
2607 final Map<SourceString, Element> elements; 2641 final Map<SourceString, Element> elements;
2608 2642
2609 MethodScope(Scope parent, Element element) 2643 MethodScope(Scope parent, Element element)
2610 : super(parent, element), 2644 : super(parent, element),
2611 this.elements = new Map<SourceString, Element>() { 2645 this.elements = new Map<SourceString, Element>() {
2612 assert(parent !== null); 2646 assert(parent !== null);
2613 } 2647 }
2614 2648
2615 Element lookup(SourceString name) { 2649 Element localLookup(SourceString name) => elements[name];
2616 Element found = elements[name];
2617 if (found !== null) return found;
2618 return parent.lookup(name);
2619 }
2620 2650
2621 Element add(Element newElement) { 2651 Element add(Element newElement) {
2622 if (elements.containsKey(newElement.name)) { 2652 if (elements.containsKey(newElement.name)) {
2623 return elements[newElement.name]; 2653 return elements[newElement.name];
2624 } 2654 }
2625 elements[newElement.name] = newElement; 2655 elements[newElement.name] = newElement;
2626 return newElement; 2656 return newElement;
2627 } 2657 }
2628 2658
2629 String toString() => '$element${elements.getKeys()} > $parent'; 2659 String toString() => '$element${elements.getKeys()} > $parent';
2630 } 2660 }
2631 2661
2632 class BlockScope extends MethodScope { 2662 class BlockScope extends MethodScope {
2633 BlockScope(Scope parent) : super(parent, parent.element); 2663 BlockScope(Scope parent) : super(parent, parent.element);
2634 2664
2635 String toString() => 'block${elements.getKeys()} > $parent'; 2665 String toString() => 'block${elements.getKeys()} > $parent';
2636 } 2666 }
2637 2667
2638 /** 2668 /**
2639 * [ClassScope] defines the inner scope of a class/interface declaration in 2669 * [ClassScope] defines the inner scope of a class/interface declaration in
2640 * which declared members, declared type variables, entities in the enclosing 2670 * which declared members, declared type variables, entities in the enclosing
2641 * scope and inherited members are available, in the given order. 2671 * scope and inherited members are available, in the given order.
2642 */ 2672 */
2643 class ClassScope extends TypeDeclarationScope { 2673 class ClassScope extends TypeDeclarationScope {
2644 ClassScope(Scope parentScope, ClassElement element) 2674 ClassScope(Scope parentScope, ClassElement element)
2645 : super(parentScope, element); 2675 : super(parentScope, element);
2646 2676
2647 Element lookup(SourceString name) { 2677 Element localLookup(SourceString name) {
2648 ClassElement cls = element; 2678 ClassElement cls = element;
2649 Element result = cls.lookupLocalMember(name); 2679 Element result = cls.lookupLocalMember(name);
2650 if (result !== null) return result; 2680 if (result !== null) return result;
2651 result = super.lookup(name); 2681 return super.localLookup(name);
2652 if (result != null) return result; 2682 }
2683
2684 Element lookup(SourceString name) {
2685 Element result = localLookup(name);
2686 if (result !== null) return result;
2687 result = parent.lexicalLookup(name);
2688 if (result !== null) return result;
2689 ClassElement cls = element;
2653 return cls.lookupSuperMember(name); 2690 return cls.lookupSuperMember(name);
2654 } 2691 }
2655 2692
2656 Element add(Element newElement) { 2693 Element add(Element newElement) {
2657 throw "Cannot add an element in a class scope"; 2694 throw "Cannot add an element in a class scope";
2658 } 2695 }
2659 2696
2660 String toString() => '$element > $parent'; 2697 String toString() => '$element > $parent';
2661 } 2698 }
2662 2699
2663 class TopScope extends Scope { 2700 class TopScope extends Scope {
2664 LibraryElement get library => element; 2701 LibraryElement get library => element;
2665 2702
2666 TopScope(LibraryElement library) : super(null, library); 2703 TopScope(LibraryElement library) : super(null, library);
2667 Element lookup(SourceString name) { 2704
2668 return library.find(name); 2705 Element localLookup(SourceString name) => library.find(name);
2669 } 2706 Element lookup(SourceString name) => localLookup(name);
2707 Element lexicalLookup(SourceString name) => localLookup(name);
2670 2708
2671 Element add(Element newElement) { 2709 Element add(Element newElement) {
2672 throw "Cannot add an element in the top scope"; 2710 throw "Cannot add an element in the top scope";
2673 } 2711 }
2674 String toString() => '$element'; 2712 String toString() => '$element';
2675 } 2713 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/lib/mock.dart ('k') | lib/compiler/implementation/scanner/keyword.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698