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

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

Issue 10911007: Rename Type to DartType to avoid conflicts with the class Type in the core library. (Closed) Base URL: https://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 Type getType(TypeAnnotation annotation); 8 DartType getType(TypeAnnotation annotation);
9 } 9 }
10 10
11 class TreeElementMapping implements TreeElements { 11 class TreeElementMapping implements TreeElements {
12 Map<Node, Element> map; 12 Map<Node, Element> map;
13 Map<Node, Selector> selectors; 13 Map<Node, Selector> selectors;
14 Map<TypeAnnotation, Type> types; 14 Map<TypeAnnotation, DartType> types;
15 15
16 TreeElementMapping() 16 TreeElementMapping()
17 : map = new LinkedHashMap<Node, Element>(), 17 : map = new LinkedHashMap<Node, Element>(),
18 selectors = new LinkedHashMap<Node, Selector>(), 18 selectors = new LinkedHashMap<Node, Selector>(),
19 types = new LinkedHashMap<TypeAnnotation, Type>(); 19 types = new LinkedHashMap<TypeAnnotation, DartType>();
20 20
21 operator []=(Node node, Element element) => map[node] = element; 21 operator []=(Node node, Element element) => map[node] = element;
22 operator [](Node node) => map[node]; 22 operator [](Node node) => map[node];
23 void remove(Node node) { map.remove(node); } 23 void remove(Node node) { map.remove(node); }
24 24
25 void setType(TypeAnnotation annotation, Type type) { 25 void setType(TypeAnnotation annotation, DartType type) {
26 types[annotation] = type; 26 types[annotation] = type;
27 } 27 }
28 28
29 Type getType(TypeAnnotation annotation) => types[annotation]; 29 DartType getType(TypeAnnotation annotation) => types[annotation];
30 30
31 void setSelector(Node node, Selector selector) { 31 void setSelector(Node node, Selector selector) {
32 selectors[node] = selector; 32 selectors[node] = selector;
33 } 33 }
34 34
35 Selector getSelector(Node node) => selectors[node]; 35 Selector getSelector(Node node) => selectors[node];
36 } 36 }
37 37
38 class ResolverTask extends CompilerTask { 38 class ResolverTask extends CompilerTask {
39 ResolverTask(Compiler compiler) : super(compiler); 39 ResolverTask(Compiler compiler) : super(compiler);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 143
144 void visitBody(ResolverVisitor visitor, Statement body) { 144 void visitBody(ResolverVisitor visitor, Statement body) {
145 visitor.visit(body); 145 visitor.visit(body);
146 } 146 }
147 147
148 void resolveConstructorImplementation(FunctionElement constructor, 148 void resolveConstructorImplementation(FunctionElement constructor,
149 FunctionExpression node) { 149 FunctionExpression node) {
150 if (constructor.defaultImplementation !== constructor) return; 150 if (constructor.defaultImplementation !== constructor) return;
151 ClassElement intrface = constructor.getEnclosingClass(); 151 ClassElement intrface = constructor.getEnclosingClass();
152 if (!intrface.isInterface()) return; 152 if (!intrface.isInterface()) return;
153 Type defaultType = intrface.defaultClass; 153 DartType defaultType = intrface.defaultClass;
154 if (defaultType === null) { 154 if (defaultType === null) {
155 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]); 155 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]);
156 } 156 }
157 ClassElement defaultClass = defaultType.element; 157 ClassElement defaultClass = defaultType.element;
158 defaultClass.ensureResolved(compiler); 158 defaultClass.ensureResolved(compiler);
159 assert(defaultClass.resolutionState == STATE_DONE); 159 assert(defaultClass.resolutionState == STATE_DONE);
160 assert(defaultClass.supertypeLoadState == STATE_DONE); 160 assert(defaultClass.supertypeLoadState == STATE_DONE);
161 if (defaultClass.isInterface()) { 161 if (defaultClass.isInterface()) {
162 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE, 162 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE,
163 [defaultClass.name]); 163 [defaultClass.name]);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 } 197 }
198 198
199 TreeElements resolveParameter(Element element) { 199 TreeElements resolveParameter(Element element) {
200 Node tree = element.parseNode(compiler); 200 Node tree = element.parseNode(compiler);
201 ResolverVisitor visitor = 201 ResolverVisitor visitor =
202 new ResolverVisitor(compiler, element.enclosingElement); 202 new ResolverVisitor(compiler, element.enclosingElement);
203 initializerDo(tree, visitor.visit); 203 initializerDo(tree, visitor.visit);
204 return visitor.mapping; 204 return visitor.mapping;
205 } 205 }
206 206
207 Type resolveTypeAnnotation(Element element, TypeAnnotation annotation) { 207 DartType resolveTypeAnnotation(Element element, TypeAnnotation annotation) {
208 if (annotation === null) return compiler.types.dynamicType; 208 if (annotation === null) return compiler.types.dynamicType;
209 ResolverVisitor visitor = new ResolverVisitor(compiler, element); 209 ResolverVisitor visitor = new ResolverVisitor(compiler, element);
210 Type result = visitor.resolveTypeAnnotation(annotation); 210 DartType result = visitor.resolveTypeAnnotation(annotation);
211 if (result === null) { 211 if (result === null) {
212 // TODO(karklose): warning. 212 // TODO(karklose): warning.
213 return compiler.types.dynamicType; 213 return compiler.types.dynamicType;
214 } 214 }
215 return result; 215 return result;
216 } 216 }
217 217
218 /** 218 /**
219 * Load and resolve the supertypes of [cls]. 219 * Load and resolve the supertypes of [cls].
220 * 220 *
221 * Warning: do not call this method directly. It should only be 221 * Warning: do not call this method directly. It should only be
222 * called by [resolveClass] and [ClassSupertypeResolver]. 222 * called by [resolveClass] and [ClassSupertypeResolver].
223 */ 223 */
224 void loadSupertypes(ClassElement cls, Node from) { 224 void loadSupertypes(ClassElement cls, Node from) {
225 compiler.withCurrentElement(cls, () => measure(() { 225 compiler.withCurrentElement(cls, () => measure(() {
226 if (cls.supertypeLoadState == STATE_DONE) return; 226 if (cls.supertypeLoadState == STATE_DONE) return;
227 if (cls.supertypeLoadState == STATE_STARTED) { 227 if (cls.supertypeLoadState == STATE_STARTED) {
228 compiler.reportMessage( 228 compiler.reportMessage(
229 compiler.spanFromNode(from), 229 compiler.spanFromNode(from),
230 MessageKind.CYCLIC_CLASS_HIERARCHY.error([cls.name]), 230 MessageKind.CYCLIC_CLASS_HIERARCHY.error([cls.name]),
231 api.Diagnostic.ERROR); 231 api.Diagnostic.ERROR);
232 cls.supertypeLoadState = STATE_DONE; 232 cls.supertypeLoadState = STATE_DONE;
233 cls.allSupertypes = const EmptyLink<Type>().prepend( 233 cls.allSupertypes = const EmptyLink<DartType>().prepend(
234 compiler.objectClass.computeType(compiler)); 234 compiler.objectClass.computeType(compiler));
235 // TODO(ahe): We should also set cls.supertype here to avoid 235 // TODO(ahe): We should also set cls.supertype here to avoid
236 // creating a malformed class hierarchy. 236 // creating a malformed class hierarchy.
237 return; 237 return;
238 } 238 }
239 cls.supertypeLoadState = STATE_STARTED; 239 cls.supertypeLoadState = STATE_STARTED;
240 compiler.withCurrentElement(cls, () { 240 compiler.withCurrentElement(cls, () {
241 // TODO(ahe): Cache the node in cls. 241 // TODO(ahe): Cache the node in cls.
242 cls.parseNode(compiler).accept(new ClassSupertypeResolver(compiler, 242 cls.parseNode(compiler).accept(new ClassSupertypeResolver(compiler,
243 cls)); 243 cls));
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 visitor.visit(node); 420 visitor.visit(node);
421 421
422 element.isBeingResolved = false; 422 element.isBeingResolved = false;
423 element.isResolved = true; 423 element.isResolved = true;
424 }); 424 });
425 }); 425 });
426 } 426 }
427 427
428 FunctionType computeFunctionType(Element element, 428 FunctionType computeFunctionType(Element element,
429 FunctionSignature signature) { 429 FunctionSignature signature) {
430 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>(); 430 LinkBuilder<DartType> parameterTypes = new LinkBuilder<DartType>();
431 for (Link<Element> link = signature.requiredParameters; 431 for (Link<Element> link = signature.requiredParameters;
432 !link.isEmpty(); 432 !link.isEmpty();
433 link = link.tail) { 433 link = link.tail) {
434 parameterTypes.addLast(link.head.computeType(compiler)); 434 parameterTypes.addLast(link.head.computeType(compiler));
435 // TODO(karlklose): optional parameters. 435 // TODO(karlklose): optional parameters.
436 } 436 }
437 return new FunctionType(signature.returnType, 437 return new FunctionType(signature.returnType,
438 parameterTypes.toLink(), 438 parameterTypes.toLink(),
439 element); 439 element);
440 } 440 }
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 } 835 }
836 } else { 836 } else {
837 return scope.lookup(typeName.source); 837 return scope.lookup(typeName.source);
838 } 838 }
839 } 839 }
840 840
841 // TODO(johnniwinther): Change [onFailure] and [whenResolved] to use boolean 841 // TODO(johnniwinther): Change [onFailure] and [whenResolved] to use boolean
842 // flags instead of closures. 842 // flags instead of closures.
843 // TODO(johnniwinther): Should never return [null] but instead an erroneous 843 // TODO(johnniwinther): Should never return [null] but instead an erroneous
844 // type. 844 // type.
845 Type resolveTypeAnnotation(TypeAnnotation node, 845 DartType resolveTypeAnnotation(TypeAnnotation node,
846 [Scope inScope, ClassElement inClass, 846 [Scope inScope, ClassElement inClass,
847 onFailure(Node, MessageKind, [List arguments]), 847 onFailure(Node, MessageKind, [List arguments]),
848 whenResolved(Node, Type)]) { 848 whenResolved(Node, Type)]) {
849 if (onFailure === null) { 849 if (onFailure === null) {
850 onFailure = (n, k, [arguments]) {}; 850 onFailure = (n, k, [arguments]) {};
851 } 851 }
852 if (whenResolved === null) { 852 if (whenResolved === null) {
853 whenResolved = (n, t) {}; 853 whenResolved = (n, t) {};
854 } 854 }
855 if (inClass !== null) { 855 if (inClass !== null) {
856 inScope = inClass.buildScope(); 856 inScope = inClass.buildScope();
857 } 857 }
858 if (inScope === null) { 858 if (inScope === null) {
859 compiler.internalError('resolveTypeAnnotation: no scope specified'); 859 compiler.internalError('resolveTypeAnnotation: no scope specified');
860 } 860 }
861 return resolveTypeAnnotationInContext(inScope, node, onFailure, 861 return resolveTypeAnnotationInContext(inScope, node, onFailure,
862 whenResolved); 862 whenResolved);
863 } 863 }
864 864
865 Type resolveTypeAnnotationInContext(Scope scope, TypeAnnotation node, 865 DartType resolveTypeAnnotationInContext(Scope scope, TypeAnnotation node,
866 onFailure, whenResolved) { 866 onFailure, whenResolved) {
867 Element element = resolveTypeName(scope, node); 867 Element element = resolveTypeName(scope, node);
868 Type type; 868 DartType type;
869 if (element === null) { 869 if (element === null) {
870 onFailure(node, MessageKind.CANNOT_RESOLVE_TYPE, [node.typeName]); 870 onFailure(node, MessageKind.CANNOT_RESOLVE_TYPE, [node.typeName]);
871 } else if (!element.impliesType()) { 871 } else if (!element.impliesType()) {
872 onFailure(node, MessageKind.NOT_A_TYPE, [node.typeName]); 872 onFailure(node, MessageKind.NOT_A_TYPE, [node.typeName]);
873 } else { 873 } else {
874 if (element === compiler.types.voidType.element || 874 if (element === compiler.types.voidType.element ||
875 element === compiler.types.dynamicType.element) { 875 element === compiler.types.dynamicType.element) {
876 type = element.computeType(compiler); 876 type = element.computeType(compiler);
877 } else if (element.isClass()) { 877 } else if (element.isClass()) {
878 ClassElement cls = element; 878 ClassElement cls = element;
879 cls.ensureResolved(compiler); 879 cls.ensureResolved(compiler);
880 Link<Type> arguments = 880 Link<DartType> arguments =
881 resolveTypeArguments(node, cls.typeVariables, scope, 881 resolveTypeArguments(node, cls.typeVariables, scope,
882 onFailure, whenResolved); 882 onFailure, whenResolved);
883 if (cls.typeVariables.isEmpty() && arguments.isEmpty()) { 883 if (cls.typeVariables.isEmpty() && arguments.isEmpty()) {
884 // Use the canonical type if it has no type parameters. 884 // Use the canonical type if it has no type parameters.
885 type = cls.computeType(compiler); 885 type = cls.computeType(compiler);
886 } else { 886 } else {
887 type = new InterfaceType(cls, arguments); 887 type = new InterfaceType(cls, arguments);
888 } 888 }
889 } else if (element.isTypedef()) { 889 } else if (element.isTypedef()) {
890 TypedefElement typdef = element; 890 TypedefElement typdef = element;
891 // TODO(ahe): Should be [ensureResolved]. 891 // TODO(ahe): Should be [ensureResolved].
892 compiler.resolveTypedef(typdef); 892 compiler.resolveTypedef(typdef);
893 typdef.computeType(compiler); 893 typdef.computeType(compiler);
894 Link<Type> arguments = resolveTypeArguments( 894 Link<DartType> arguments = resolveTypeArguments(
895 node, typdef.typeVariables, 895 node, typdef.typeVariables,
896 scope, onFailure, whenResolved); 896 scope, onFailure, whenResolved);
897 if (typdef.typeVariables.isEmpty() && arguments.isEmpty()) { 897 if (typdef.typeVariables.isEmpty() && arguments.isEmpty()) {
898 // Return the canonical type if it has no type parameters. 898 // Return the canonical type if it has no type parameters.
899 type = typdef.computeType(compiler); 899 type = typdef.computeType(compiler);
900 } else { 900 } else {
901 type = new TypedefType(typdef, arguments); 901 type = new TypedefType(typdef, arguments);
902 } 902 }
903 } else if (element.isTypeVariable()) { 903 } else if (element.isTypeVariable()) {
904 type = element.computeType(compiler); 904 type = element.computeType(compiler);
905 } else { 905 } else {
906 compiler.cancel("unexpected element kind ${element.kind}", 906 compiler.cancel("unexpected element kind ${element.kind}",
907 node: node); 907 node: node);
908 } 908 }
909 } 909 }
910 whenResolved(node, type); 910 whenResolved(node, type);
911 return type; 911 return type;
912 } 912 }
913 913
914 Link<Type> resolveTypeArguments(TypeAnnotation node, 914 Link<DartType> resolveTypeArguments(TypeAnnotation node,
915 Link<Type> typeVariables, 915 Link<DartType> typeVariables,
916 Scope scope, onFailure, whenResolved) { 916 Scope scope, onFailure, whenResolved) {
917 if (node.typeArguments == null) { 917 if (node.typeArguments == null) {
918 return const EmptyLink<Type>(); 918 return const EmptyLink<DartType>();
919 } 919 }
920 var arguments = new LinkBuilder<Type>(); 920 var arguments = new LinkBuilder<DartType>();
921 for (Link<Node> typeArguments = node.typeArguments.nodes; 921 for (Link<Node> typeArguments = node.typeArguments.nodes;
922 !typeArguments.isEmpty(); 922 !typeArguments.isEmpty();
923 typeArguments = typeArguments.tail) { 923 typeArguments = typeArguments.tail) {
924 if (typeVariables.isEmpty()) { 924 if (typeVariables.isEmpty()) {
925 onFailure(typeArguments.head, MessageKind.ADDITIONAL_TYPE_ARGUMENT); 925 onFailure(typeArguments.head, MessageKind.ADDITIONAL_TYPE_ARGUMENT);
926 } 926 }
927 Type argType = resolveTypeAnnotationInContext(scope, 927 DartType argType = resolveTypeAnnotationInContext(scope,
928 typeArguments.head, 928 typeArguments.head,
929 onFailure, 929 onFailure,
930 whenResolved); 930 whenResolved);
931 arguments.addLast(argType); 931 arguments.addLast(argType);
932 if (!typeVariables.isEmpty()) { 932 if (!typeVariables.isEmpty()) {
933 typeVariables = typeVariables.tail; 933 typeVariables = typeVariables.tail;
934 } 934 }
935 } 935 }
936 if (!typeVariables.isEmpty()) { 936 if (!typeVariables.isEmpty()) {
937 onFailure(node.typeArguments, MessageKind.MISSING_TYPE_ARGUMENT); 937 onFailure(node.typeArguments, MessageKind.MISSING_TYPE_ARGUMENT);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1022 if ((element.kind.category & allowedCategory) == 0) { 1022 if ((element.kind.category & allowedCategory) == 0) {
1023 // TODO(ahe): Improve error message. Need UX input. 1023 // TODO(ahe): Improve error message. Need UX input.
1024 error(node, MessageKind.GENERIC, ["is not an expression $element"]); 1024 error(node, MessageKind.GENERIC, ["is not an expression $element"]);
1025 } 1025 }
1026 } 1026 }
1027 return useElement(node, element); 1027 return useElement(node, element);
1028 } 1028 }
1029 } 1029 }
1030 1030
1031 Element visitTypeAnnotation(TypeAnnotation node) { 1031 Element visitTypeAnnotation(TypeAnnotation node) {
1032 Type type = resolveTypeAnnotation(node); 1032 DartType type = resolveTypeAnnotation(node);
1033 if (type !== null) return type.element; 1033 if (type !== null) return type.element;
1034 return null; 1034 return null;
1035 } 1035 }
1036 1036
1037 Element defineElement(Node node, Element element, 1037 Element defineElement(Node node, Element element,
1038 [bool doAddToScope = true]) { 1038 [bool doAddToScope = true]) {
1039 compiler.ensure(element !== null); 1039 compiler.ensure(element !== null);
1040 mapping[node] = element; 1040 mapping[node] = element;
1041 if (doAddToScope) { 1041 if (doAddToScope) {
1042 Element existing = scope.add(element); 1042 Element existing = scope.add(element);
1043 if (existing != element) { 1043 if (existing != element) {
1044 error(node, MessageKind.DUPLICATE_DEFINITION, [node]); 1044 error(node, MessageKind.DUPLICATE_DEFINITION, [node]);
1045 } 1045 }
1046 } 1046 }
1047 return element; 1047 return element;
1048 } 1048 }
1049 1049
1050 Element useElement(Node node, Element element) { 1050 Element useElement(Node node, Element element) {
1051 if (element === null) return null; 1051 if (element === null) return null;
1052 return mapping[node] = element; 1052 return mapping[node] = element;
1053 } 1053 }
1054 1054
1055 Type useType(TypeAnnotation annotation, Type type) { 1055 DartType useType(TypeAnnotation annotation, DartType type) {
1056 if (type !== null) { 1056 if (type !== null) {
1057 mapping.setType(annotation, type); 1057 mapping.setType(annotation, type);
1058 useElement(annotation, type.element); 1058 useElement(annotation, type.element);
1059 } 1059 }
1060 return type; 1060 return type;
1061 } 1061 }
1062 1062
1063 void setupFunction(FunctionExpression node, FunctionElement function) { 1063 void setupFunction(FunctionExpression node, FunctionElement function) {
1064 scope = new MethodScope(scope, function); 1064 scope = new MethodScope(scope, function);
1065 // Put the parameters in scope. 1065 // Put the parameters in scope.
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1235 } else if (resolvedReceiver.kind === ElementKind.PREFIX) { 1235 } else if (resolvedReceiver.kind === ElementKind.PREFIX) {
1236 PrefixElement prefix = resolvedReceiver; 1236 PrefixElement prefix = resolvedReceiver;
1237 target = prefix.lookupLocalMember(name); 1237 target = prefix.lookupLocalMember(name);
1238 if (target == null) { 1238 if (target == null) {
1239 error(node, MessageKind.NO_SUCH_LIBRARY_MEMBER, [prefix.name, name]); 1239 error(node, MessageKind.NO_SUCH_LIBRARY_MEMBER, [prefix.name, name]);
1240 } 1240 }
1241 } 1241 }
1242 return target; 1242 return target;
1243 } 1243 }
1244 1244
1245 Type resolveTypeTest(Node argument) { 1245 DartType resolveTypeTest(Node argument) {
1246 TypeAnnotation node = argument.asTypeAnnotation(); 1246 TypeAnnotation node = argument.asTypeAnnotation();
1247 if (node === null) { 1247 if (node === null) {
1248 // node is of the form !Type. 1248 // node is of the form !Type.
1249 node = argument.asSend().receiver.asTypeAnnotation(); 1249 node = argument.asSend().receiver.asTypeAnnotation();
1250 if (node === null) compiler.cancel("malformed send"); 1250 if (node === null) compiler.cancel("malformed send");
1251 } 1251 }
1252 return resolveTypeRequired(node); 1252 return resolveTypeRequired(node);
1253 } 1253 }
1254 1254
1255 static Selector computeSendSelector(Send node, LibraryElement library) { 1255 static Selector computeSendSelector(Send node, LibraryElement library) {
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
1578 } else { 1578 } else {
1579 // Resolve and store the type this annotation resolves to. The type 1579 // Resolve and store the type this annotation resolves to. The type
1580 // is used in the backend, e.g., for creating runtime type information. 1580 // is used in the backend, e.g., for creating runtime type information.
1581 // TODO(karlklose): This will resolve the class element again. Refactor 1581 // TODO(karlklose): This will resolve the class element again. Refactor
1582 // so we can use the TypeResolver. 1582 // so we can use the TypeResolver.
1583 resolveTypeRequired(annotation); 1583 resolveTypeRequired(annotation);
1584 } 1584 }
1585 return constructor; 1585 return constructor;
1586 } 1586 }
1587 1587
1588 Type resolveTypeRequired(TypeAnnotation node) { 1588 DartType resolveTypeRequired(TypeAnnotation node) {
1589 bool old = typeRequired; 1589 bool old = typeRequired;
1590 typeRequired = true; 1590 typeRequired = true;
1591 Type result = resolveTypeAnnotation(node); 1591 DartType result = resolveTypeAnnotation(node);
1592 typeRequired = old; 1592 typeRequired = old;
1593 return result; 1593 return result;
1594 } 1594 }
1595 1595
1596 Type resolveTypeAnnotation(TypeAnnotation node) { 1596 DartType resolveTypeAnnotation(TypeAnnotation node) {
1597 Function report = typeRequired ? error : warning; 1597 Function report = typeRequired ? error : warning;
1598 return typeResolver.resolveTypeAnnotation(node, inScope: scope, 1598 return typeResolver.resolveTypeAnnotation(node, inScope: scope,
1599 onFailure: report, 1599 onFailure: report,
1600 whenResolved: useType); 1600 whenResolved: useType);
1601 } 1601 }
1602 1602
1603 visitModifiers(Modifiers node) { 1603 visitModifiers(Modifiers node) {
1604 // TODO(ngeoffray): Implement this. 1604 // TODO(ngeoffray): Implement this.
1605 unimplemented(node, 'modifiers'); 1605 unimplemented(node, 'modifiers');
1606 } 1606 }
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
1851 visitIn(node.type, blockScope); 1851 visitIn(node.type, blockScope);
1852 visitIn(node.formals, blockScope); 1852 visitIn(node.formals, blockScope);
1853 visitIn(node.block, blockScope); 1853 visitIn(node.block, blockScope);
1854 } 1854 }
1855 1855
1856 visitTypedef(Typedef node) { 1856 visitTypedef(Typedef node) {
1857 unimplemented(node, 'typedef'); 1857 unimplemented(node, 'typedef');
1858 } 1858 }
1859 } 1859 }
1860 1860
1861 class TypeDefinitionVisitor extends CommonResolverVisitor<Type> { 1861 class TypeDefinitionVisitor extends CommonResolverVisitor<DartType> {
1862 Scope scope; 1862 Scope scope;
1863 TypeDeclarationElement element; 1863 TypeDeclarationElement element;
1864 TypeResolver typeResolver; 1864 TypeResolver typeResolver;
1865 1865
1866 TypeDefinitionVisitor(Compiler compiler, TypeDeclarationElement element) 1866 TypeDefinitionVisitor(Compiler compiler, TypeDeclarationElement element)
1867 : this.element = element, 1867 : this.element = element,
1868 scope = element.buildEnclosingScope(), 1868 scope = element.buildEnclosingScope(),
1869 typeResolver = new TypeResolver(compiler), 1869 typeResolver = new TypeResolver(compiler),
1870 super(compiler); 1870 super(compiler);
1871 1871
1872 void resolveTypeVariableBounds(NodeList node) { 1872 void resolveTypeVariableBounds(NodeList node) {
1873 if (node === null) return; 1873 if (node === null) return;
1874 1874
1875 var nameSet = new Set<SourceString>(); 1875 var nameSet = new Set<SourceString>();
1876 // Resolve the bounds of type variables. 1876 // Resolve the bounds of type variables.
1877 Link<Type> typeLink = element.typeVariables; 1877 Link<DartType> typeLink = element.typeVariables;
1878 Link<Node> nodeLink = node.nodes; 1878 Link<Node> nodeLink = node.nodes;
1879 while (!nodeLink.isEmpty()) { 1879 while (!nodeLink.isEmpty()) {
1880 TypeVariableType typeVariable = typeLink.head; 1880 TypeVariableType typeVariable = typeLink.head;
1881 SourceString typeName = typeVariable.name; 1881 SourceString typeName = typeVariable.name;
1882 TypeVariable typeNode = nodeLink.head; 1882 TypeVariable typeNode = nodeLink.head;
1883 if (nameSet.contains(typeName)) { 1883 if (nameSet.contains(typeName)) {
1884 error(typeNode, MessageKind.DUPLICATE_TYPE_VARIABLE_NAME, [typeName]); 1884 error(typeNode, MessageKind.DUPLICATE_TYPE_VARIABLE_NAME, [typeName]);
1885 } 1885 }
1886 nameSet.add(typeName); 1886 nameSet.add(typeName);
1887 1887
1888 TypeVariableElement variableElement = typeVariable.element; 1888 TypeVariableElement variableElement = typeVariable.element;
1889 if (typeNode.bound !== null) { 1889 if (typeNode.bound !== null) {
1890 Type boundType = typeResolver.resolveTypeAnnotation( 1890 DartType boundType = typeResolver.resolveTypeAnnotation(
1891 typeNode.bound, inScope: scope, onFailure: warning); 1891 typeNode.bound, inScope: scope, onFailure: warning);
1892 if (boundType !== null && boundType.element == variableElement) { 1892 if (boundType !== null && boundType.element == variableElement) {
1893 // TODO(johnniwinther): Check for more general cycles, like 1893 // TODO(johnniwinther): Check for more general cycles, like
1894 // [: <A extends B, B extends C, C extends B> :]. 1894 // [: <A extends B, B extends C, C extends B> :].
1895 warning(node, MessageKind.CYCLIC_TYPE_VARIABLE, 1895 warning(node, MessageKind.CYCLIC_TYPE_VARIABLE,
1896 [variableElement.name]); 1896 [variableElement.name]);
1897 } else if (boundType !== null) { 1897 } else if (boundType !== null) {
1898 variableElement.bound = boundType; 1898 variableElement.bound = boundType;
1899 } else { 1899 } else {
1900 // TODO(johnniwinther): Should be an erroneous type. 1900 // TODO(johnniwinther): Should be an erroneous type.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1942 * resolved, but it cannot call [ResolverTask.resolveClass] directly 1942 * resolved, but it cannot call [ResolverTask.resolveClass] directly
1943 * or indirectly (through [ClassElement.ensureResolved]) for any other 1943 * or indirectly (through [ClassElement.ensureResolved]) for any other
1944 * types. 1944 * types.
1945 */ 1945 */
1946 class ClassResolverVisitor extends TypeDefinitionVisitor { 1946 class ClassResolverVisitor extends TypeDefinitionVisitor {
1947 ClassElement get element => super.element; 1947 ClassElement get element => super.element;
1948 1948
1949 ClassResolverVisitor(Compiler compiler, ClassElement classElement) 1949 ClassResolverVisitor(Compiler compiler, ClassElement classElement)
1950 : super(compiler, classElement); 1950 : super(compiler, classElement);
1951 1951
1952 Type visitClassNode(ClassNode node) { 1952 DartType visitClassNode(ClassNode node) {
1953 compiler.ensure(element !== null); 1953 compiler.ensure(element !== null);
1954 compiler.ensure(element.resolutionState == STATE_STARTED); 1954 compiler.ensure(element.resolutionState == STATE_STARTED);
1955 1955
1956 InterfaceType type = element.computeType(compiler); 1956 InterfaceType type = element.computeType(compiler);
1957 scope = new TypeDeclarationScope(scope, element); 1957 scope = new TypeDeclarationScope(scope, element);
1958 // TODO(ahe): It is not safe to call resolveTypeVariableBounds yet. 1958 // TODO(ahe): It is not safe to call resolveTypeVariableBounds yet.
1959 // As a side-effect, this may get us back here trying to 1959 // As a side-effect, this may get us back here trying to
1960 // resolve this class again. 1960 // resolve this class again.
1961 resolveTypeVariableBounds(node.typeParameters); 1961 resolveTypeVariableBounds(node.typeParameters);
1962 1962
1963 // Find super type. 1963 // Find super type.
1964 Type supertype = visit(node.superclass); 1964 DartType supertype = visit(node.superclass);
1965 if (supertype !== null && supertype.element.isExtendable()) { 1965 if (supertype !== null && supertype.element.isExtendable()) {
1966 element.supertype = supertype; 1966 element.supertype = supertype;
1967 if (isBlackListed(supertype)) { 1967 if (isBlackListed(supertype)) {
1968 error(node.superclass, MessageKind.CANNOT_EXTEND, [supertype]); 1968 error(node.superclass, MessageKind.CANNOT_EXTEND, [supertype]);
1969 } 1969 }
1970 } else if (supertype !== null) { 1970 } else if (supertype !== null) {
1971 error(node.superclass, MessageKind.TYPE_NAME_EXPECTED); 1971 error(node.superclass, MessageKind.TYPE_NAME_EXPECTED);
1972 } 1972 }
1973 final objectElement = compiler.objectClass; 1973 final objectElement = compiler.objectClass;
1974 if (element !== objectElement && element.supertype === null) { 1974 if (element !== objectElement && element.supertype === null) {
1975 if (objectElement === null) { 1975 if (objectElement === null) {
1976 compiler.internalError("Internal error: cannot resolve Object", 1976 compiler.internalError("Internal error: cannot resolve Object",
1977 node: node); 1977 node: node);
1978 } else { 1978 } else {
1979 objectElement.ensureResolved(compiler); 1979 objectElement.ensureResolved(compiler);
1980 } 1980 }
1981 // TODO(ahe): This should be objectElement.computeType(...). 1981 // TODO(ahe): This should be objectElement.computeType(...).
1982 element.supertype = new InterfaceType(objectElement); 1982 element.supertype = new InterfaceType(objectElement);
1983 } 1983 }
1984 assert(element.interfaces === null); 1984 assert(element.interfaces === null);
1985 Link<Type> interfaces = const EmptyLink<Type>(); 1985 Link<DartType> interfaces = const EmptyLink<DartType>();
1986 for (Link<Node> link = node.interfaces.nodes; 1986 for (Link<Node> link = node.interfaces.nodes;
1987 !link.isEmpty(); 1987 !link.isEmpty();
1988 link = link.tail) { 1988 link = link.tail) {
1989 Type interfaceType = visit(link.head); 1989 DartType interfaceType = visit(link.head);
1990 if (interfaceType !== null && interfaceType.element.isExtendable()) { 1990 if (interfaceType !== null && interfaceType.element.isExtendable()) {
1991 interfaces = interfaces.prepend(interfaceType); 1991 interfaces = interfaces.prepend(interfaceType);
1992 if (isBlackListed(interfaceType)) { 1992 if (isBlackListed(interfaceType)) {
1993 error(link.head, MessageKind.CANNOT_IMPLEMENT, [interfaceType]); 1993 error(link.head, MessageKind.CANNOT_IMPLEMENT, [interfaceType]);
1994 } 1994 }
1995 } else { 1995 } else {
1996 error(link.head, MessageKind.TYPE_NAME_EXPECTED); 1996 error(link.head, MessageKind.TYPE_NAME_EXPECTED);
1997 } 1997 }
1998 } 1998 }
1999 element.interfaces = interfaces; 1999 element.interfaces = interfaces;
2000 calculateAllSupertypes(element); 2000 calculateAllSupertypes(element);
2001 2001
2002 if (node.defaultClause !== null) { 2002 if (node.defaultClause !== null) {
2003 element.defaultClass = visit(node.defaultClause); 2003 element.defaultClass = visit(node.defaultClause);
2004 } 2004 }
2005 addDefaultConstructorIfNeeded(element); 2005 addDefaultConstructorIfNeeded(element);
2006 return element.computeType(compiler); 2006 return element.computeType(compiler);
2007 } 2007 }
2008 2008
2009 Type visitTypeAnnotation(TypeAnnotation node) { 2009 DartType visitTypeAnnotation(TypeAnnotation node) {
2010 return visit(node.typeName); 2010 return visit(node.typeName);
2011 } 2011 }
2012 2012
2013 Type visitIdentifier(Identifier node) { 2013 DartType visitIdentifier(Identifier node) {
2014 Element element = scope.lookup(node.source); 2014 Element element = scope.lookup(node.source);
2015 if (element === null) { 2015 if (element === null) {
2016 error(node, MessageKind.CANNOT_RESOLVE_TYPE, [node]); 2016 error(node, MessageKind.CANNOT_RESOLVE_TYPE, [node]);
2017 return null; 2017 return null;
2018 } else if (!element.impliesType() && !element.isTypeVariable()) { 2018 } else if (!element.impliesType() && !element.isTypeVariable()) {
2019 error(node, MessageKind.NOT_A_TYPE, [node]); 2019 error(node, MessageKind.NOT_A_TYPE, [node]);
2020 return null; 2020 return null;
2021 } else { 2021 } else {
2022 if (element.isTypeVariable()) { 2022 if (element.isTypeVariable()) {
2023 TypeVariableElement variableElement = element; 2023 TypeVariableElement variableElement = element;
2024 return variableElement.type; 2024 return variableElement.type;
2025 } else if (element.isTypedef()) { 2025 } else if (element.isTypedef()) {
2026 compiler.unimplemented('visitIdentifier for typedefs', node: node); 2026 compiler.unimplemented('visitIdentifier for typedefs', node: node);
2027 } else { 2027 } else {
2028 // TODO(ngeoffray): Use type variables. 2028 // TODO(ngeoffray): Use type variables.
2029 return element.computeType(compiler); 2029 return element.computeType(compiler);
2030 } 2030 }
2031 } 2031 }
2032 return null; 2032 return null;
2033 } 2033 }
2034 2034
2035 Type visitSend(Send node) { 2035 DartType visitSend(Send node) {
2036 Identifier prefix = node.receiver.asIdentifier(); 2036 Identifier prefix = node.receiver.asIdentifier();
2037 if (prefix === null) { 2037 if (prefix === null) {
2038 error(node.receiver, MessageKind.NOT_A_PREFIX, [node.receiver]); 2038 error(node.receiver, MessageKind.NOT_A_PREFIX, [node.receiver]);
2039 return null; 2039 return null;
2040 } 2040 }
2041 Element element = scope.lookup(prefix.source); 2041 Element element = scope.lookup(prefix.source);
2042 if (element === null || element.kind !== ElementKind.PREFIX) { 2042 if (element === null || element.kind !== ElementKind.PREFIX) {
2043 error(node.receiver, MessageKind.NOT_A_PREFIX, [node.receiver]); 2043 error(node.receiver, MessageKind.NOT_A_PREFIX, [node.receiver]);
2044 return null; 2044 return null;
2045 } 2045 }
2046 PrefixElement prefixElement = element; 2046 PrefixElement prefixElement = element;
2047 Identifier selector = node.selector.asIdentifier(); 2047 Identifier selector = node.selector.asIdentifier();
2048 var e = prefixElement.lookupLocalMember(selector.source); 2048 var e = prefixElement.lookupLocalMember(selector.source);
2049 if (e === null || !e.impliesType()) { 2049 if (e === null || !e.impliesType()) {
2050 error(node.selector, MessageKind.CANNOT_RESOLVE_TYPE, [node.selector]); 2050 error(node.selector, MessageKind.CANNOT_RESOLVE_TYPE, [node.selector]);
2051 return null; 2051 return null;
2052 } 2052 }
2053 return e.computeType(compiler); 2053 return e.computeType(compiler);
2054 } 2054 }
2055 2055
2056 void calculateAllSupertypes(ClassElement cls) { 2056 void calculateAllSupertypes(ClassElement cls) {
2057 // TODO(karlklose): substitute type variables. 2057 // TODO(karlklose): substitute type variables.
2058 // TODO(karlklose): check if type arguments match, if a classelement occurs 2058 // TODO(karlklose): check if type arguments match, if a classelement occurs
2059 // more than once in the supertypes. 2059 // more than once in the supertypes.
2060 if (cls.allSupertypes !== null) return; 2060 if (cls.allSupertypes !== null) return;
2061 final Type supertype = cls.supertype; 2061 final DartType supertype = cls.supertype;
2062 if (supertype != null) { 2062 if (supertype != null) {
2063 ClassElement superElement = supertype.element; 2063 ClassElement superElement = supertype.element;
2064 Link<Type> superSupertypes = superElement.allSupertypes; 2064 Link<DartType> superSupertypes = superElement.allSupertypes;
2065 assert(superSupertypes !== null); 2065 assert(superSupertypes !== null);
2066 Link<Type> supertypes = new Link<Type>(supertype, superSupertypes); 2066 Link<DartType> supertypes =
2067 for (Link<Type> interfaces = cls.interfaces; 2067 new Link<DartType>(supertype, superSupertypes);
2068 for (Link<DartType> interfaces = cls.interfaces;
2068 !interfaces.isEmpty(); 2069 !interfaces.isEmpty();
2069 interfaces = interfaces.tail) { 2070 interfaces = interfaces.tail) {
2070 ClassElement element = interfaces.head.element; 2071 ClassElement element = interfaces.head.element;
2071 Link<Type> interfaceSupertypes = element.allSupertypes; 2072 Link<DartType> interfaceSupertypes = element.allSupertypes;
2072 assert(interfaceSupertypes !== null); 2073 assert(interfaceSupertypes !== null);
2073 supertypes = supertypes.reversePrependAll(interfaceSupertypes); 2074 supertypes = supertypes.reversePrependAll(interfaceSupertypes);
2074 supertypes = supertypes.prepend(interfaces.head); 2075 supertypes = supertypes.prepend(interfaces.head);
2075 } 2076 }
2076 cls.allSupertypes = supertypes; 2077 cls.allSupertypes = supertypes;
2077 } else { 2078 } else {
2078 assert(cls === compiler.objectClass); 2079 assert(cls === compiler.objectClass);
2079 cls.allSupertypes = const EmptyLink<Type>(); 2080 cls.allSupertypes = const EmptyLink<DartType>();
2080 } 2081 }
2081 } 2082 }
2082 2083
2083 /** 2084 /**
2084 * Add a synthetic nullary constructor if there are no other 2085 * Add a synthetic nullary constructor if there are no other
2085 * constructors. 2086 * constructors.
2086 */ 2087 */
2087 void addDefaultConstructorIfNeeded(ClassElement element) { 2088 void addDefaultConstructorIfNeeded(ClassElement element) {
2088 if (element.hasConstructor) return; 2089 if (element.hasConstructor) return;
2089 SynthesizedConstructorElement constructor = 2090 SynthesizedConstructorElement constructor =
2090 new SynthesizedConstructorElement(element); 2091 new SynthesizedConstructorElement(element);
2091 element.addToScope(constructor, compiler); 2092 element.addToScope(constructor, compiler);
2092 Type returnType = compiler.types.voidType; 2093 DartType returnType = compiler.types.voidType;
2093 constructor.type = new FunctionType(returnType, const EmptyLink<Type>(), 2094 constructor.type = new FunctionType(returnType, const EmptyLink<DartType>(),
2094 constructor); 2095 constructor);
2095 constructor.cachedNode = 2096 constructor.cachedNode =
2096 new FunctionExpression(new Identifier(element.position()), 2097 new FunctionExpression(new Identifier(element.position()),
2097 new NodeList.empty(), 2098 new NodeList.empty(),
2098 new Block(new NodeList.empty()), 2099 new Block(new NodeList.empty()),
2099 null, null, null, null); 2100 null, null, null, null);
2100 } 2101 }
2101 2102
2102 isBlackListed(Type type) { 2103 isBlackListed(DartType type) {
2103 LibraryElement lib = element.getLibrary(); 2104 LibraryElement lib = element.getLibrary();
2104 return 2105 return
2105 lib !== compiler.coreLibrary && 2106 lib !== compiler.coreLibrary &&
2106 lib !== compiler.coreImplLibrary && 2107 lib !== compiler.coreImplLibrary &&
2107 lib !== compiler.jsHelperLibrary && 2108 lib !== compiler.jsHelperLibrary &&
2108 (type.element === compiler.dynamicClass || 2109 (type.element === compiler.dynamicClass ||
2109 type.element === compiler.boolClass || 2110 type.element === compiler.boolClass ||
2110 type.element === compiler.numClass || 2111 type.element === compiler.numClass ||
2111 type.element === compiler.intClass || 2112 type.element === compiler.intClass ||
2112 type.element === compiler.doubleClass || 2113 type.element === compiler.doubleClass ||
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
2272 return new VariableElement(node.source, variables, 2273 return new VariableElement(node.source, variables,
2273 ElementKind.PARAMETER, enclosingElement, node: node); 2274 ElementKind.PARAMETER, enclosingElement, node: node);
2274 } 2275 }
2275 2276
2276 SourceString getParameterName(Send node) { 2277 SourceString getParameterName(Send node) {
2277 var identifier = node.selector.asIdentifier(); 2278 var identifier = node.selector.asIdentifier();
2278 if (identifier !== null) { 2279 if (identifier !== null) {
2279 // Normal parameter: [:Type name:]. 2280 // Normal parameter: [:Type name:].
2280 return identifier.source; 2281 return identifier.source;
2281 } else { 2282 } else {
2282 // Function type parameter: [:void name(Type arg):]. 2283 // Function type parameter: [:void name(DartType arg):].
2283 var functionExpression = node.selector.asFunctionExpression(); 2284 var functionExpression = node.selector.asFunctionExpression();
2284 if (functionExpression !== null && 2285 if (functionExpression !== null &&
2285 functionExpression.name.asIdentifier() !== null) { 2286 functionExpression.name.asIdentifier() !== null) {
2286 return functionExpression.name.asIdentifier().source; 2287 return functionExpression.name.asIdentifier().source;
2287 } else { 2288 } else {
2288 cancel(node, 2289 cancel(node,
2289 'internal error: unimplemented receiver on parameter send'); 2290 'internal error: unimplemented receiver on parameter send');
2290 } 2291 }
2291 } 2292 }
2292 } 2293 }
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
2381 MessageKind.EXTRA_FORMALS.error([]), 2382 MessageKind.EXTRA_FORMALS.error([]),
2382 api.Diagnostic.WARNING); 2383 api.Diagnostic.WARNING);
2383 } 2384 }
2384 } 2385 }
2385 } 2386 }
2386 LinkBuilder<Element> parametersBuilder = 2387 LinkBuilder<Element> parametersBuilder =
2387 visitor.analyzeNodes(formalParameters.nodes); 2388 visitor.analyzeNodes(formalParameters.nodes);
2388 requiredParameterCount = parametersBuilder.length; 2389 requiredParameterCount = parametersBuilder.length;
2389 parameters = parametersBuilder.toLink(); 2390 parameters = parametersBuilder.toLink();
2390 } 2391 }
2391 Type returnType = compiler.resolveTypeAnnotation(element, returnNode); 2392 DartType returnType = compiler.resolveTypeAnnotation(element, returnNode);
2392 return new FunctionSignature(parameters, 2393 return new FunctionSignature(parameters,
2393 visitor.optionalParameters, 2394 visitor.optionalParameters,
2394 requiredParameterCount, 2395 requiredParameterCount,
2395 visitor.optionalParameterCount, 2396 visitor.optionalParameterCount,
2396 returnType); 2397 returnType);
2397 } 2398 }
2398 2399
2399 // TODO(ahe): This is temporary. 2400 // TODO(ahe): This is temporary.
2400 void resolveExpression(Node node) { 2401 void resolveExpression(Node node) {
2401 if (node == null) return; 2402 if (node == null) return;
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
2542 } 2543 }
2543 2544
2544 /** 2545 /**
2545 * Looks up [name] within the type variables declared in [element]. 2546 * Looks up [name] within the type variables declared in [element].
2546 */ 2547 */
2547 Element lookupTypeVariable(SourceString name) { 2548 Element lookupTypeVariable(SourceString name) {
2548 return null; 2549 return null;
2549 } 2550 }
2550 2551
2551 Element lookup(SourceString name) { 2552 Element lookup(SourceString name) {
2552 Link<Type> typeVariableLink = element.typeVariables; 2553 Link<DartType> typeVariableLink = element.typeVariables;
2553 while (!typeVariableLink.isEmpty()) { 2554 while (!typeVariableLink.isEmpty()) {
2554 TypeVariableType typeVariable = typeVariableLink.head; 2555 TypeVariableType typeVariable = typeVariableLink.head;
2555 if (typeVariable.name == name) { 2556 if (typeVariable.name == name) {
2556 return typeVariable.element; 2557 return typeVariable.element;
2557 } 2558 }
2558 typeVariableLink = typeVariableLink.tail; 2559 typeVariableLink = typeVariableLink.tail;
2559 } 2560 }
2560 2561
2561 return parent.lookup(name); 2562 return parent.lookup(name);
2562 } 2563 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
2628 TopScope(LibraryElement library) : super(null, library); 2629 TopScope(LibraryElement library) : super(null, library);
2629 Element lookup(SourceString name) { 2630 Element lookup(SourceString name) {
2630 return library.find(name); 2631 return library.find(name);
2631 } 2632 }
2632 2633
2633 Element add(Element newElement) { 2634 Element add(Element newElement) {
2634 throw "Cannot add an element in the top scope"; 2635 throw "Cannot add an element in the top scope";
2635 } 2636 }
2636 String toString() => '$element'; 2637 String toString() => '$element';
2637 } 2638 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698