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

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

Issue 10910098: Re-apply with a few fixes http://codereview.chromium.org/10913081/: Fix resolution of type paramete… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: 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 434 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 parameterTypes.toLink(), 445 parameterTypes.toLink(),
446 element); 446 element);
447 } 447 }
448 448
449 void resolveMetadataAnnotation(PartialMetadataAnnotation annotation) { 449 void resolveMetadataAnnotation(PartialMetadataAnnotation annotation) {
450 compiler.withCurrentElement(annotation.annotatedElement, () => measure(() { 450 compiler.withCurrentElement(annotation.annotatedElement, () => measure(() {
451 assert(annotation.resolutionState == STATE_NOT_STARTED); 451 assert(annotation.resolutionState == STATE_NOT_STARTED);
452 annotation.resolutionState = STATE_STARTED; 452 annotation.resolutionState = STATE_STARTED;
453 453
454 Node node = annotation.parseNode(compiler); 454 Node node = annotation.parseNode(compiler);
455 ResolverVisitor visitor = 455 ResolverVisitor visitor = new ResolverVisitor(
456 new ResolverVisitor(compiler, annotation.annotatedElement); 456 compiler, annotation.annotatedElement.enclosingElement);
457 node.accept(visitor); 457 node.accept(visitor);
458 annotation.value = compiler.constantHandler.compileNodeWithDefinitions( 458 annotation.value = compiler.constantHandler.compileNodeWithDefinitions(
459 node, visitor.mapping); 459 node, visitor.mapping);
460 460
461 annotation.resolutionState = STATE_DONE; 461 annotation.resolutionState = STATE_DONE;
462 })); 462 }));
463 } 463 }
464 464
465 error(Node node, MessageKind kind, [arguments = const []]) { 465 error(Node node, MessageKind kind, [arguments = const []]) {
466 ResolutionError message = new ResolutionError(kind, arguments); 466 ResolutionError message = new ResolutionError(kind, arguments);
(...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 this.enclosingElement = element, 964 this.enclosingElement = element,
965 // When the element is a field, we are actually resolving its 965 // When the element is a field, we are actually resolving its
966 // initial value, which should not have access to instance 966 // initial value, which should not have access to instance
967 // fields. 967 // fields.
968 inInstanceContext = (element.isInstanceMember() && !element.isField()) 968 inInstanceContext = (element.isInstanceMember() && !element.isField())
969 || element.isGenerativeConstructor(), 969 || element.isGenerativeConstructor(),
970 this.currentClass = element.isMember() ? element.getEnclosingClass() 970 this.currentClass = element.isMember() ? element.getEnclosingClass()
971 : null, 971 : null,
972 this.statementScope = new StatementScope(), 972 this.statementScope = new StatementScope(),
973 typeResolver = new TypeResolver(compiler), 973 typeResolver = new TypeResolver(compiler),
974 scope = element.buildEnclosingScope(), 974 scope = element.buildScope(),
975 inCheckContext = compiler.enableTypeAssertions, 975 inCheckContext = compiler.enableTypeAssertions,
976 super(compiler) { 976 super(compiler);
977 }
978 977
979 Enqueuer get world => compiler.enqueuer.resolution; 978 Enqueuer get world => compiler.enqueuer.resolution;
980 979
981 Element lookup(Node node, SourceString name) { 980 Element lookup(Node node, SourceString name) {
982 Element result = scope.lookup(name); 981 Element result = scope.lookup(name);
983 if (!inInstanceContext && result != null && result.isInstanceMember()) { 982 if (!inInstanceContext && result != null && result.isInstanceMember()) {
984 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); 983 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]);
985 } 984 }
986 return result; 985 return result;
987 } 986 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
1075 1074
1076 DartType useType(TypeAnnotation annotation, DartType type) { 1075 DartType useType(TypeAnnotation annotation, DartType type) {
1077 if (type !== null) { 1076 if (type !== null) {
1078 mapping.setType(annotation, type); 1077 mapping.setType(annotation, type);
1079 useElement(annotation, type.element); 1078 useElement(annotation, type.element);
1080 } 1079 }
1081 return type; 1080 return type;
1082 } 1081 }
1083 1082
1084 void setupFunction(FunctionExpression node, FunctionElement function) { 1083 void setupFunction(FunctionExpression node, FunctionElement function) {
1085 scope = new MethodScope(scope, function); 1084 // If [function] is the [enclosingElement], the [scope] has
1085 // already been set in the constructor of [ResolverVisitor].
1086 if (function != enclosingElement) scope = new MethodScope(scope, function);
1087
1086 // Put the parameters in scope. 1088 // Put the parameters in scope.
1087 FunctionSignature functionParameters = 1089 FunctionSignature functionParameters =
1088 function.computeSignature(compiler); 1090 function.computeSignature(compiler);
1089 Link<Node> parameterNodes = (node.parameters === null) 1091 Link<Node> parameterNodes = (node.parameters === null)
1090 ? const EmptyLink<Node>() : node.parameters.nodes; 1092 ? const EmptyLink<Node>() : node.parameters.nodes;
1091 functionParameters.forEachParameter((Element element) { 1093 functionParameters.forEachParameter((Element element) {
1092 if (element == functionParameters.optionalParameters.head) { 1094 if (element == functionParameters.optionalParameters.head) {
1093 NodeList nodes = parameterNodes.head; 1095 NodeList nodes = parameterNodes.head;
1094 parameterNodes = nodes.nodes; 1096 parameterNodes = nodes.nodes;
1095 } 1097 }
(...skipping 1478 matching lines...) Expand 10 before | Expand all | Expand 10 after
2574 2576
2575 class Scope { 2577 class Scope {
2576 final Element element; 2578 final Element element;
2577 final Scope parent; 2579 final Scope parent;
2578 2580
2579 Scope(this.parent, this.element); 2581 Scope(this.parent, this.element);
2580 abstract Element add(Element element); 2582 abstract Element add(Element element);
2581 abstract Element lookup(SourceString name); 2583 abstract Element lookup(SourceString name);
2582 } 2584 }
2583 2585
2586 class VariableScope extends Scope {
2587 VariableScope(parent, element) : super(parent, element);
2588
2589 Element add(Element newElement) {
2590 throw "Cannot add element to VariableScope";
2591 }
2592
2593 Element lookup(SourceString name) => parent.lookup(name);
2594
2595 String toString() => '$element > $parent';
2596 }
2597
2584 /** 2598 /**
2585 * [TypeDeclarationScope] defines the outer scope of a type declaration in 2599 * [TypeDeclarationScope] defines the outer scope of a type declaration in
2586 * 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
2587 * available but where declared and inherited members are not available. This 2601 * available but where declared and inherited members are not available. This
2588 * 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
2589 * class hierarchy. In all other cases [ClassScope] is used. 2603 * class hierarchy. In all other cases [ClassScope] is used.
2590 */ 2604 */
2591 class TypeDeclarationScope extends Scope { 2605 class TypeDeclarationScope extends Scope {
2592 TypeDeclarationElement get element => super.element; 2606 TypeDeclarationElement get element => super.element;
2593 2607
2594 TypeDeclarationScope(parent, TypeDeclarationElement element) 2608 TypeDeclarationScope(parent, TypeDeclarationElement element)
2595 : super(parent, element) { 2609 : super(parent, element) {
2596 assert(parent !== null); 2610 assert(parent !== null);
2597 } 2611 }
2598 2612
2599 Element add(Element newElement) { 2613 Element add(Element newElement) {
2600 throw "Cannot add element to TypeDeclarationScope"; 2614 throw "Cannot add element to TypeDeclarationScope";
2601 } 2615 }
2602 2616
2603 /**
2604 * Looks up [name] within the type variables declared in [element].
2605 */
2606 Element lookupTypeVariable(SourceString name) {
2607 return null;
2608 }
2609
2610 Element lookup(SourceString name) { 2617 Element lookup(SourceString name) {
2611 Link<DartType> typeVariableLink = element.typeVariables; 2618 Link<DartType> typeVariableLink = element.typeVariables;
2612 while (!typeVariableLink.isEmpty()) { 2619 while (!typeVariableLink.isEmpty()) {
2613 TypeVariableType typeVariable = typeVariableLink.head; 2620 TypeVariableType typeVariable = typeVariableLink.head;
2614 if (typeVariable.name == name) { 2621 if (typeVariable.name == name) {
2615 return typeVariable.element; 2622 return typeVariable.element;
2616 } 2623 }
2617 typeVariableLink = typeVariableLink.tail; 2624 typeVariableLink = typeVariableLink.tail;
2618 } 2625 }
2619 2626
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2655 2662
2656 String toString() => 'block${elements.getKeys()} > $parent'; 2663 String toString() => 'block${elements.getKeys()} > $parent';
2657 } 2664 }
2658 2665
2659 /** 2666 /**
2660 * [ClassScope] defines the inner scope of a class/interface declaration in 2667 * [ClassScope] defines the inner scope of a class/interface declaration in
2661 * which declared members, declared type variables, entities in the enclosing 2668 * which declared members, declared type variables, entities in the enclosing
2662 * scope and inherited members are available, in the given order. 2669 * scope and inherited members are available, in the given order.
2663 */ 2670 */
2664 class ClassScope extends TypeDeclarationScope { 2671 class ClassScope extends TypeDeclarationScope {
2672 bool inStaticContext = false;
2673
2665 ClassScope(Scope parentScope, ClassElement element) 2674 ClassScope(Scope parentScope, ClassElement element)
2666 : super(parentScope, element); 2675 : super(parentScope, element);
2667 2676
2668 Element lookup(SourceString name) { 2677 Element lookup(SourceString name) {
2669 ClassElement cls = element; 2678 ClassElement cls = element;
2670 Element result = cls.lookupLocalMember(name); 2679 Element result = cls.lookupLocalMember(name);
2671 if (result !== null) return result; 2680 if (result !== null) return result;
2672 result = super.lookup(name); 2681 if (!inStaticContext) {
2682 // If not in a static context, we can lookup in the
2683 // TypeDeclaration scope, which contains the type variables of
2684 // the class.
2685 result = super.lookup(name);
2686 } else {
2687 result = parent.lookup(name);
2688 }
2673 if (result != null) return result; 2689 if (result != null) return result;
2674 return cls.lookupSuperMember(name); 2690 return cls.lookupSuperMember(name);
2675 } 2691 }
2676 2692
2677 Element add(Element newElement) { 2693 Element add(Element newElement) {
2678 throw "Cannot add an element in a class scope"; 2694 throw "Cannot add an element in a class scope";
2679 } 2695 }
2680 2696
2681 String toString() => '$element > $parent'; 2697 String toString() => '$element > $parent';
2682 } 2698 }
2683 2699
2684 class TopScope extends Scope { 2700 class TopScope extends Scope {
2685 LibraryElement get library => element; 2701 LibraryElement get library => element;
2686 2702
2687 TopScope(LibraryElement library) : super(null, library); 2703 TopScope(LibraryElement library) : super(null, library);
2688 Element lookup(SourceString name) { 2704 Element lookup(SourceString name) {
2689 return library.find(name); 2705 return library.find(name);
2690 } 2706 }
2691 2707
2692 Element add(Element newElement) { 2708 Element add(Element newElement) {
2693 throw "Cannot add an element in the top scope"; 2709 throw "Cannot add an element in the top scope";
2694 } 2710 }
2695 String toString() => '$element'; 2711 String toString() => '$element';
2696 } 2712 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698