| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return node.selector.asIdentifier().source; | 63 return node.selector.asIdentifier().source; |
| 64 } else { | 64 } else { |
| 65 return const SourceString(''); | 65 return const SourceString(''); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 FunctionElement lookupConstructor(ClassElement classElement, Send send, | 69 FunctionElement lookupConstructor(ClassElement classElement, Send send, |
| 70 [noConstructor(Element)]) { | 70 [noConstructor(Element)]) { |
| 71 final SourceString constructorName = getConstructorName(send); | 71 final SourceString constructorName = getConstructorName(send); |
| 72 final SourceString className = classElement.name; | 72 final SourceString className = classElement.name; |
| 73 FunctionElement result = classElement.lookupConstructor(className, | 73 return classElement.lookupConstructor(className, |
| 74 constructorName, | 74 constructorName, |
| 75 noConstructor); | 75 noConstructor); |
| 76 if (result === null && send.arguments.isEmpty()) { | |
| 77 result = classElement.getSynthesizedConstructor(); | |
| 78 } | |
| 79 return result; | |
| 80 } | 76 } |
| 81 | 77 |
| 82 FunctionElement resolveConstructorRedirection(FunctionElement constructor) { | 78 FunctionElement resolveConstructorRedirection(FunctionElement constructor) { |
| 83 FunctionExpression node = constructor.parseNode(compiler); | 79 FunctionExpression node = constructor.parseNode(compiler); |
| 84 // A synthetic constructor does not have a node. | 80 // A synthetic constructor does not have a node. |
| 85 if (node === null) return null; | 81 if (node === null) return null; |
| 86 if (node.initializers === null) return null; | 82 if (node.initializers === null) return null; |
| 87 Link<Node> initializers = node.initializers.nodes; | 83 Link<Node> initializers = node.initializers.nodes; |
| 88 if (!initializers.isEmpty() && | 84 if (!initializers.isEmpty() && |
| 89 Initializers.isConstructorRedirect(initializers.head)) { | 85 Initializers.isConstructorRedirect(initializers.head)) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 defaultClass.lookupConstructor(constructor.name); | 171 defaultClass.lookupConstructor(constructor.name); |
| 176 | 172 |
| 177 // If that fails, try looking up "MyClass.name". | 173 // If that fails, try looking up "MyClass.name". |
| 178 if (constructor.defaultImplementation === null) { | 174 if (constructor.defaultImplementation === null) { |
| 179 SourceString name = | 175 SourceString name = |
| 180 new SourceString(constructor.name.slowToString().replaceFirst( | 176 new SourceString(constructor.name.slowToString().replaceFirst( |
| 181 intrface.name.slowToString(), | 177 intrface.name.slowToString(), |
| 182 defaultClass.name.slowToString())); | 178 defaultClass.name.slowToString())); |
| 183 constructor.defaultImplementation = defaultClass.lookupConstructor(name); | 179 constructor.defaultImplementation = defaultClass.lookupConstructor(name); |
| 184 | 180 |
| 185 if (constructor.defaultImplementation === null | |
| 186 && name == defaultClass.name | |
| 187 && constructor.computeParameters(compiler).parameterCount === 0) { | |
| 188 constructor.defaultImplementation = | |
| 189 defaultClass.getSynthesizedConstructor(); | |
| 190 } | |
| 191 | |
| 192 if (constructor.defaultImplementation === null) { | 181 if (constructor.defaultImplementation === null) { |
| 193 // We failed find a constrcutor named either | 182 // We failed find a constrcutor named either |
| 194 // "MyInterface.name" or "MyClass.name". | 183 // "MyInterface.name" or "MyClass.name". |
| 195 error(node, MessageKind.CANNOT_FIND_CONSTRUCTOR2, | 184 error(node, MessageKind.CANNOT_FIND_CONSTRUCTOR2, |
| 196 [constructor.name, name]); | 185 [constructor.name, name]); |
| 197 } | 186 } |
| 198 } | 187 } |
| 199 } | 188 } |
| 200 | 189 |
| 201 TreeElements resolveVariableElement(Element element) { | 190 TreeElements resolveVariableElement(Element element) { |
| (...skipping 992 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1194 element.supertype = new SimpleType(Types.OBJECT, objectElement); | 1183 element.supertype = new SimpleType(Types.OBJECT, objectElement); |
| 1195 } | 1184 } |
| 1196 if (node.defaultClause !== null) { | 1185 if (node.defaultClause !== null) { |
| 1197 element.defaultClass = visit(node.defaultClause.nodes.head); | 1186 element.defaultClass = visit(node.defaultClause.nodes.head); |
| 1198 } | 1187 } |
| 1199 for (Link<Node> link = node.interfaces.nodes; | 1188 for (Link<Node> link = node.interfaces.nodes; |
| 1200 !link.isEmpty(); | 1189 !link.isEmpty(); |
| 1201 link = link.tail) { | 1190 link = link.tail) { |
| 1202 element.interfaces = element.interfaces.prepend(visit(link.head)); | 1191 element.interfaces = element.interfaces.prepend(visit(link.head)); |
| 1203 } | 1192 } |
| 1193 |
| 1194 if (element.constructors.length == 0) { |
| 1195 // Add a synthetic nullary constructor if there are no other |
| 1196 // constructors. |
| 1197 SynthesizedConstructorElement constructor = |
| 1198 new SynthesizedConstructorElement(element); |
| 1199 element.constructors[element.name] = constructor; |
| 1200 Type returnType = compiler.types.voidType; |
| 1201 constructor.type = new FunctionType(returnType, const EmptyLink<Type>(), |
| 1202 constructor); |
| 1203 constructor.cachedNode = |
| 1204 new FunctionExpression(new Identifier(element.position()), |
| 1205 new NodeList.empty(), |
| 1206 new Block(new NodeList.empty()), |
| 1207 null, null, null, null); |
| 1208 } |
| 1209 |
| 1204 return element.computeType(compiler); | 1210 return element.computeType(compiler); |
| 1205 } | 1211 } |
| 1206 | 1212 |
| 1207 Type visitTypeAnnotation(TypeAnnotation node) { | 1213 Type visitTypeAnnotation(TypeAnnotation node) { |
| 1208 return visit(node.typeName); | 1214 return visit(node.typeName); |
| 1209 } | 1215 } |
| 1210 | 1216 |
| 1211 Type visitIdentifier(Identifier node) { | 1217 Type visitIdentifier(Identifier node) { |
| 1212 Element element = context.lookup(node.source); | 1218 Element element = context.lookup(node.source); |
| 1213 if (element === null) { | 1219 if (element === null) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1437 visitNewExpression(NewExpression node) { | 1443 visitNewExpression(NewExpression node) { |
| 1438 Node selector = node.send.selector; | 1444 Node selector = node.send.selector; |
| 1439 Element e = visit(selector); | 1445 Element e = visit(selector); |
| 1440 if (e !== null && e.kind === ElementKind.CLASS) { | 1446 if (e !== null && e.kind === ElementKind.CLASS) { |
| 1441 ClassElement cls = e; | 1447 ClassElement cls = e; |
| 1442 cls.resolve(compiler); | 1448 cls.resolve(compiler); |
| 1443 compiler.resolver.toResolve.add(cls); | 1449 compiler.resolver.toResolve.add(cls); |
| 1444 if (cls.isInterface() && (cls.defaultClass === null)) { | 1450 if (cls.isInterface() && (cls.defaultClass === null)) { |
| 1445 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); | 1451 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); |
| 1446 } | 1452 } |
| 1447 FunctionElement constructor = cls.lookupConstructor(cls.name); | 1453 e = cls.lookupConstructor(cls.name); |
| 1448 if (constructor === null && node.send.argumentsNode.isEmpty()) { | |
| 1449 e = cls.getSynthesizedConstructor(); | |
| 1450 } else { | |
| 1451 e = constructor; | |
| 1452 } | |
| 1453 } | 1454 } |
| 1454 return e; | 1455 return e; |
| 1455 } | 1456 } |
| 1456 | 1457 |
| 1457 visitTypeAnnotation(TypeAnnotation node) { | 1458 visitTypeAnnotation(TypeAnnotation node) { |
| 1458 // TODO(ahe): Do not ignore type arguments. | 1459 // TODO(ahe): Do not ignore type arguments. |
| 1459 return visit(node.typeName); | 1460 return visit(node.typeName); |
| 1460 } | 1461 } |
| 1461 | 1462 |
| 1462 visitSend(Send node) { | 1463 visitSend(Send node) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1564 class TopScope extends Scope { | 1565 class TopScope extends Scope { |
| 1565 LibraryElement get library() => element; | 1566 LibraryElement get library() => element; |
| 1566 | 1567 |
| 1567 TopScope(LibraryElement library) : super(null, library); | 1568 TopScope(LibraryElement library) : super(null, library); |
| 1568 Element lookup(SourceString name) => library.find(name); | 1569 Element lookup(SourceString name) => library.find(name); |
| 1569 | 1570 |
| 1570 Element add(Element element) { | 1571 Element add(Element element) { |
| 1571 throw "Cannot add an element in the top scope"; | 1572 throw "Cannot add an element in the top scope"; |
| 1572 } | 1573 } |
| 1573 } | 1574 } |
| OLD | NEW |