| 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 } | 8 } |
| 9 | 9 |
| 10 class TreeElementMapping implements TreeElements { | 10 class TreeElementMapping implements TreeElements { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 return resolveVariableElement(element); | 51 return resolveVariableElement(element); |
| 52 | 52 |
| 53 default: | 53 default: |
| 54 compiler.unimplemented( | 54 compiler.unimplemented( |
| 55 "resolver", node: element.parseNode(compiler)); | 55 "resolver", node: element.parseNode(compiler)); |
| 56 } | 56 } |
| 57 }); | 57 }); |
| 58 } | 58 } |
| 59 | 59 |
| 60 TreeElements resolveMethodElement(FunctionElement element) { | 60 TreeElements resolveMethodElement(FunctionElement element) { |
| 61 if (element.kind === ElementKind.GENERATIVE_CONSTRUCTOR && | 61 return compiler.withCurrentElement(element, () { |
| 62 constructorElements[element] !== null) { | 62 bool isConstructor = element.kind === ElementKind.GENERATIVE_CONSTRUCTOR; |
| 63 return constructorElements[element]; | 63 if (isConstructor) { |
| 64 TreeElements elements = constructorElements[element]; |
| 65 if (elements !== null) return elements; |
| 66 } |
| 67 FunctionExpression tree = element.parseNode(compiler); |
| 68 if (isConstructor) { |
| 69 resolveConstructorImplementation(element, tree); |
| 70 } |
| 71 ResolverVisitor visitor = new ResolverVisitor(compiler, element); |
| 72 visitor.useElement(tree, element); |
| 73 visitor.setupFunction(tree, element); |
| 74 |
| 75 if (tree.initializers != null) { |
| 76 new InitializerResolver(visitor, element).resolveInitializers(tree); |
| 77 } |
| 78 visitor.visit(tree.body); |
| 79 |
| 80 // Resolve the type annotations encountered in the method. |
| 81 Link<ClassElement> newResolvedClasses = const EmptyLink<ClassElement>(); |
| 82 while (!toResolve.isEmpty()) { |
| 83 ClassElement classElement = toResolve.removeFirst(); |
| 84 if (!classElement.isResolved) { |
| 85 classElement.resolve(compiler); |
| 86 } |
| 87 newResolvedClasses = newResolvedClasses.prepend(classElement); |
| 88 } |
| 89 checkClassHierarchy(newResolvedClasses); |
| 90 if (isConstructor) { |
| 91 constructorElements[element] = visitor.mapping; |
| 92 } |
| 93 return visitor.mapping; |
| 94 }); |
| 95 } |
| 96 |
| 97 void resolveConstructorImplementation(FunctionElement constructor, |
| 98 FunctionExpression node) { |
| 99 assert(constructor.defaultImplementation === constructor); |
| 100 ClassElement intrface = constructor.enclosingElement; |
| 101 if (!intrface.isInterface()) return; |
| 102 Type defaultType = intrface.defaultClass; |
| 103 if (defaultType === null) { |
| 104 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]); |
| 64 } | 105 } |
| 65 FunctionExpression tree = element.parseNode(compiler); | 106 ClassElement defaultClass = defaultType.element; |
| 66 ResolverVisitor visitor = new ResolverVisitor(compiler, element); | 107 defaultClass.resolve(compiler); |
| 67 visitor.useElement(tree, element); | 108 if (defaultClass.isInterface()) { |
| 68 visitor.setupFunction(tree, element); | 109 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE, |
| 110 [defaultClass.name]); |
| 111 } |
| 112 // We have now established the following: |
| 113 // [intrface] is an interface, let's say "MyInterface". |
| 114 // [defaultClass] is a class, let's say "MyClass". |
| 69 | 115 |
| 70 if (tree.initializers != null) { | 116 // First look up the constructor named "MyInterface.name". |
| 71 new InitializerResolver(visitor, element).resolveInitializers(tree); | 117 constructor.defaultImplementation = |
| 118 defaultClass.lookupConstructor(constructor.name); |
| 119 |
| 120 // If that fails, try looking up "MyClass.name". |
| 121 if (constructor.defaultImplementation === null) { |
| 122 SourceString name = |
| 123 new SourceString(constructor.name.toString().replaceFirst( |
| 124 intrface.name.toString(), |
| 125 defaultClass.name.toString())); |
| 126 constructor.defaultImplementation = defaultClass.lookupConstructor(name); |
| 127 |
| 128 if (constructor.defaultImplementation === null |
| 129 && name == defaultClass.name |
| 130 && constructor.computeParameters(compiler).parameterCount === 0) { |
| 131 constructor.defaultImplementation = |
| 132 defaultClass.getSynthesizedConstructor(); |
| 133 } |
| 134 |
| 135 if (constructor.defaultImplementation === null) { |
| 136 // We failed find a constrcutor named either |
| 137 // "MyInterface.name" or "MyClass.name". |
| 138 error(node, MessageKind.CANNOT_FIND_CONSTRUCTOR2, |
| 139 [constructor.name, name]); |
| 140 } |
| 72 } | 141 } |
| 73 visitor.visit(tree.body); | |
| 74 | |
| 75 // Resolve the type annotations encountered in the method. | |
| 76 Link<ClassElement> newResolvedClasses = const EmptyLink<ClassElement>(); | |
| 77 while (!toResolve.isEmpty()) { | |
| 78 ClassElement classElement = toResolve.removeFirst(); | |
| 79 if (!classElement.isResolved) { | |
| 80 classElement.resolve(compiler); | |
| 81 } | |
| 82 newResolvedClasses = newResolvedClasses.prepend(classElement); | |
| 83 } | |
| 84 checkClassHierarchy(newResolvedClasses); | |
| 85 if (element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { | |
| 86 constructorElements[element] = visitor.mapping; | |
| 87 } | |
| 88 return visitor.mapping; | |
| 89 } | 142 } |
| 90 | 143 |
| 91 TreeElements resolveVariableElement(Element element) { | 144 TreeElements resolveVariableElement(Element element) { |
| 92 Node tree = element.parseNode(compiler); | 145 Node tree = element.parseNode(compiler); |
| 93 ResolverVisitor visitor = new ResolverVisitor(compiler, element); | 146 ResolverVisitor visitor = new ResolverVisitor(compiler, element); |
| 94 if (tree is SendSet) { | 147 if (tree is SendSet) { |
| 95 SendSet send = tree; | 148 SendSet send = tree; |
| 96 visitor.visit(send.arguments.head); | 149 visitor.visit(send.arguments.head); |
| 97 } | 150 } |
| 98 return visitor.mapping; | 151 return visitor.mapping; |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 } | 852 } |
| 800 | 853 |
| 801 visitNewExpression(NewExpression node) { | 854 visitNewExpression(NewExpression node) { |
| 802 if (node.isConst()) cancel(node, 'const expressions are not implemented'); | 855 if (node.isConst()) cancel(node, 'const expressions are not implemented'); |
| 803 Node selector = node.send.selector; | 856 Node selector = node.send.selector; |
| 804 if (selector.asTypeAnnotation() === null) { | 857 if (selector.asTypeAnnotation() === null) { |
| 805 cancel( | 858 cancel( |
| 806 node, 'named constructors with type arguments are not implemented'); | 859 node, 'named constructors with type arguments are not implemented'); |
| 807 } | 860 } |
| 808 | 861 |
| 862 FunctionElement constructor = resolveConstructor(node); |
| 863 handleArguments(node.send); |
| 864 if (constructor === null) return null; |
| 865 // TODO(karlklose): handle optional arguments. |
| 866 if (node.send.argumentCount() != constructor.parameterCount(compiler)) { |
| 867 // TODO(ngeoffray): resolution error with wrong number of |
| 868 // parameters. We cannot do this rigth now because of the |
| 869 // List constructor. |
| 870 } |
| 871 useElement(node.send, constructor); |
| 872 return null; |
| 873 } |
| 874 |
| 875 FunctionElement resolveConstructor(NewExpression node) { |
| 809 SourceString constructorName; | 876 SourceString constructorName; |
| 877 Node selector = node.send.selector; |
| 810 Node typeName = selector.asTypeAnnotation().typeName; | 878 Node typeName = selector.asTypeAnnotation().typeName; |
| 811 if (typeName.asSend() !== null) { | 879 if (typeName.asSend() !== null) { |
| 812 Identifier receiver = typeName.asSend().receiver.asIdentifier(); | 880 SourceString className = typeName.asSend().receiver.asIdentifier().source; |
| 813 Identifier selector = typeName.asSend().selector.asIdentifier(); | 881 SourceString name = typeName.asSend().selector.asIdentifier().source; |
| 814 SourceString className = receiver.source; | |
| 815 SourceString name = selector.source; | |
| 816 constructorName = Elements.constructConstructorName(className, name); | 882 constructorName = Elements.constructConstructorName(className, name); |
| 817 } else { | 883 } else { |
| 818 constructorName = typeName.asIdentifier().source; | 884 constructorName = typeName.asIdentifier().source; |
| 819 } | 885 } |
| 820 ClassElement cls = resolveTypeRequired(selector); | 886 ClassElement cls = resolveTypeRequired(selector); |
| 821 Element constructor = null; | 887 if (cls === null) { |
| 822 if (cls !== null) { | |
| 823 cls.resolve(compiler); | |
| 824 if (cls.isInterface() && (cls.defaultClass === null)) { | |
| 825 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); | |
| 826 } | |
| 827 constructor = cls.lookupConstructor(constructorName); | |
| 828 if (constructorName == cls.name | |
| 829 && constructor === null | |
| 830 && node.send.argumentsNode.isEmpty()) { | |
| 831 constructor = cls.getSynthesizedConstructor(); | |
| 832 } | |
| 833 if (constructor === null) { | |
| 834 error(node.send, MessageKind.CANNOT_FIND_CONSTRUCTOR, [node.send]); | |
| 835 } else { | |
| 836 FunctionElement function = constructor; | |
| 837 // TODO(karlklose): handle optional arguments. | |
| 838 if (node.send.argumentCount() != function.parameterCount(compiler)) { | |
| 839 // TODO(ngeoffray): reslution error with wrong number of | |
| 840 // parameters. We cannot do this rigth now because of the | |
| 841 // List constructor. | |
| 842 } | |
| 843 } | |
| 844 } else { | |
| 845 error(selector, MessageKind.CANNOT_RESOLVE_TYPE, [selector]); | 888 error(selector, MessageKind.CANNOT_RESOLVE_TYPE, [selector]); |
| 889 return null; |
| 846 } | 890 } |
| 847 handleArguments(node.send); | 891 cls.resolve(compiler); |
| 848 useElement(node.send, constructor); | 892 if (cls.isInterface() && (cls.defaultClass === null)) { |
| 893 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); |
| 894 } |
| 895 FunctionElement constructor = cls.lookupConstructor(constructorName); |
| 896 if (constructor !== null) return constructor; |
| 897 if (constructorName == cls.name && node.send.argumentsNode.isEmpty()) { |
| 898 return cls.getSynthesizedConstructor(); |
| 899 } |
| 900 error(node.send, MessageKind.CANNOT_FIND_CONSTRUCTOR, [node.send]); |
| 849 return null; | 901 return null; |
| 850 } | 902 } |
| 851 | 903 |
| 852 ClassElement resolveTypeRequired(Node node) { | 904 ClassElement resolveTypeRequired(Node node) { |
| 853 bool old = typeRequired; | 905 bool old = typeRequired; |
| 854 typeRequired = true; | 906 typeRequired = true; |
| 855 ClassElement cls = visit(node); | 907 ClassElement cls = visit(node); |
| 856 typeRequired = old; | 908 typeRequired = old; |
| 857 return cls; | 909 return cls; |
| 858 } | 910 } |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1292 class TopScope extends Scope { | 1344 class TopScope extends Scope { |
| 1293 LibraryElement get library() => element; | 1345 LibraryElement get library() => element; |
| 1294 | 1346 |
| 1295 TopScope(LibraryElement library) : super(null, library); | 1347 TopScope(LibraryElement library) : super(null, library); |
| 1296 Element lookup(SourceString name) => library.find(name); | 1348 Element lookup(SourceString name) => library.find(name); |
| 1297 | 1349 |
| 1298 Element add(Element element) { | 1350 Element add(Element element) { |
| 1299 throw "Cannot add an element in the top scope"; | 1351 throw "Cannot add an element in the top scope"; |
| 1300 } | 1352 } |
| 1301 } | 1353 } |
| OLD | NEW |