| 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 part of resolution; | 5 part of resolution; |
| 6 | 6 |
| 7 abstract class TreeElements { | 7 abstract class TreeElements { |
| 8 Element operator[](Node node); | 8 Element operator[](Node node); |
| 9 Selector getSelector(Send send); | 9 Selector getSelector(Send send); |
| 10 DartType getType(Node node); | 10 DartType getType(Node node); |
| (...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1076 return resolveTypeAnnotationInContext(scope, node, onFailure, | 1076 return resolveTypeAnnotationInContext(scope, node, onFailure, |
| 1077 whenResolved); | 1077 whenResolved); |
| 1078 } | 1078 } |
| 1079 | 1079 |
| 1080 DartType resolveTypeAnnotationInContext(Scope scope, TypeAnnotation node, | 1080 DartType resolveTypeAnnotationInContext(Scope scope, TypeAnnotation node, |
| 1081 onFailure, whenResolved) { | 1081 onFailure, whenResolved) { |
| 1082 Element element = resolveTypeName(scope, node); | 1082 Element element = resolveTypeName(scope, node); |
| 1083 DartType type; | 1083 DartType type; |
| 1084 if (element == null) { | 1084 if (element == null) { |
| 1085 onFailure(node, MessageKind.CANNOT_RESOLVE_TYPE, [node.typeName]); | 1085 onFailure(node, MessageKind.CANNOT_RESOLVE_TYPE, [node.typeName]); |
| 1086 } else if (element.isErroneous()) { | 1086 } else if (element.isAmbiguous()) { |
| 1087 ErroneousElement error = element; | 1087 AmbiguousElement ambiguous = element; |
| 1088 onFailure(node, error.messageKind, error.messageArguments); | 1088 onFailure(node, ambiguous.messageKind, ambiguous.messageArguments); |
| 1089 } else if (!element.impliesType()) { | 1089 } else if (!element.impliesType()) { |
| 1090 onFailure(node, MessageKind.NOT_A_TYPE, [node.typeName]); | 1090 onFailure(node, MessageKind.NOT_A_TYPE, [node.typeName]); |
| 1091 } else { | 1091 } else { |
| 1092 if (identical(element, compiler.types.voidType.element) || | 1092 if (identical(element, compiler.types.voidType.element) || |
| 1093 identical(element, compiler.types.dynamicType.element)) { | 1093 identical(element, compiler.types.dynamicType.element)) { |
| 1094 type = element.computeType(compiler); | 1094 type = element.computeType(compiler); |
| 1095 } else if (element.isClass()) { | 1095 } else if (element.isClass()) { |
| 1096 ClassElement cls = element; | 1096 ClassElement cls = element; |
| 1097 cls.ensureResolved(compiler); | 1097 cls.ensureResolved(compiler); |
| 1098 Link<DartType> arguments = | 1098 Link<DartType> arguments = |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1192 typeResolver = new TypeResolver(compiler), | 1192 typeResolver = new TypeResolver(compiler), |
| 1193 scope = element.buildScope(), | 1193 scope = element.buildScope(), |
| 1194 inCheckContext = compiler.enableTypeAssertions, | 1194 inCheckContext = compiler.enableTypeAssertions, |
| 1195 inCatchBlock = false, | 1195 inCatchBlock = false, |
| 1196 super(compiler); | 1196 super(compiler); |
| 1197 | 1197 |
| 1198 Enqueuer get world => compiler.enqueuer.resolution; | 1198 Enqueuer get world => compiler.enqueuer.resolution; |
| 1199 | 1199 |
| 1200 Element lookup(Node node, SourceString name) { | 1200 Element lookup(Node node, SourceString name) { |
| 1201 Element result = scope.lookup(name); | 1201 Element result = scope.lookup(name); |
| 1202 if (!inInstanceContext && result != null && result.isInstanceMember()) { | 1202 if (!Elements.isUnresolved(result)) { |
| 1203 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); | 1203 if (!inInstanceContext && result.isInstanceMember()) { |
| 1204 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); |
| 1205 // TODO(johnniwinther): Create an ErroneousElement. |
| 1206 } else if (result.isAmbiguous()) { |
| 1207 AmbiguousElement ambiguous = result; |
| 1208 compiler.reportMessage(compiler.spanFromNode(node), |
| 1209 ambiguous.messageKind.error(ambiguous.messageArguments), |
| 1210 Diagnostic.ERROR); |
| 1211 return new ErroneousElement(ambiguous.messageKind, |
| 1212 ambiguous.messageArguments, |
| 1213 name, enclosingElement); |
| 1214 } |
| 1204 } | 1215 } |
| 1205 return result; | 1216 return result; |
| 1206 } | 1217 } |
| 1207 | 1218 |
| 1208 // Create, or reuse an already created, statement element for a statement. | 1219 // Create, or reuse an already created, statement element for a statement. |
| 1209 TargetElement getOrCreateTargetElement(Node statement) { | 1220 TargetElement getOrCreateTargetElement(Node statement) { |
| 1210 TargetElement element = mapping[statement]; | 1221 TargetElement element = mapping[statement]; |
| 1211 if (element == null) { | 1222 if (element == null) { |
| 1212 element = new TargetElement(statement, | 1223 element = new TargetElement(statement, |
| 1213 statementScope.nestingLevel, | 1224 statementScope.nestingLevel, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1234 } | 1245 } |
| 1235 | 1246 |
| 1236 visitInStaticContext(Node node) { | 1247 visitInStaticContext(Node node) { |
| 1237 inStaticContext(() => visit(node)); | 1248 inStaticContext(() => visit(node)); |
| 1238 } | 1249 } |
| 1239 | 1250 |
| 1240 ErroneousElement warnAndCreateErroneousElement(Node node, | 1251 ErroneousElement warnAndCreateErroneousElement(Node node, |
| 1241 SourceString name, | 1252 SourceString name, |
| 1242 MessageKind kind, | 1253 MessageKind kind, |
| 1243 List<Node> arguments) { | 1254 List<Node> arguments) { |
| 1244 return warnOnErroneousElement(node, | 1255 ResolutionWarning warning = new ResolutionWarning(kind, arguments); |
| 1245 new ErroneousElement(kind, arguments, name, enclosingElement)); | |
| 1246 } | |
| 1247 | |
| 1248 ErroneousElement warnOnErroneousElement(Node node, | |
| 1249 ErroneousElement erroneousElement) { | |
| 1250 ResolutionWarning warning = | |
| 1251 new ResolutionWarning(erroneousElement.messageKind, | |
| 1252 erroneousElement.messageArguments); | |
| 1253 compiler.reportWarning(node, warning); | 1256 compiler.reportWarning(node, warning); |
| 1254 return erroneousElement; | 1257 return new ErroneousElement(kind, arguments, name, enclosingElement); |
| 1255 } | 1258 } |
| 1256 | 1259 |
| 1257 Element visitIdentifier(Identifier node) { | 1260 Element visitIdentifier(Identifier node) { |
| 1258 if (node.isThis()) { | 1261 if (node.isThis()) { |
| 1259 if (!inInstanceContext) { | 1262 if (!inInstanceContext) { |
| 1260 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); | 1263 error(node, MessageKind.NO_INSTANCE_AVAILABLE, [node]); |
| 1261 } | 1264 } |
| 1262 return null; | 1265 return null; |
| 1263 } else if (node.isSuper()) { | 1266 } else if (node.isSuper()) { |
| 1264 if (!inInstanceContext) error(node, MessageKind.NO_SUPER_IN_STATIC); | 1267 if (!inInstanceContext) error(node, MessageKind.NO_SUPER_IN_STATIC); |
| 1265 if ((ElementCategory.SUPER & allowedCategory) == 0) { | 1268 if ((ElementCategory.SUPER & allowedCategory) == 0) { |
| 1266 error(node, MessageKind.INVALID_USE_OF_SUPER); | 1269 error(node, MessageKind.INVALID_USE_OF_SUPER); |
| 1267 } | 1270 } |
| 1268 return null; | 1271 return null; |
| 1269 } else { | 1272 } else { |
| 1270 Element element = lookup(node, node.source); | 1273 Element element = lookup(node, node.source); |
| 1271 if (element == null) { | 1274 if (element == null) { |
| 1272 if (!inInstanceContext) { | 1275 if (!inInstanceContext) { |
| 1273 element = warnAndCreateErroneousElement(node, node.source, | 1276 element = warnAndCreateErroneousElement(node, node.source, |
| 1274 MessageKind.CANNOT_RESOLVE, | 1277 MessageKind.CANNOT_RESOLVE, |
| 1275 [node]); | 1278 [node]); |
| 1276 } | 1279 } |
| 1277 } else if (element.isErroneous()) { | 1280 } else if (element.isErroneous()) { |
| 1278 element = warnOnErroneousElement(node, element); | 1281 // Use the erroneous element. |
| 1279 } else { | 1282 } else { |
| 1280 if ((element.kind.category & allowedCategory) == 0) { | 1283 if ((element.kind.category & allowedCategory) == 0) { |
| 1281 // TODO(ahe): Improve error message. Need UX input. | 1284 // TODO(ahe): Improve error message. Need UX input. |
| 1282 error(node, MessageKind.GENERIC, ["is not an expression $element"]); | 1285 error(node, MessageKind.GENERIC, ["is not an expression $element"]); |
| 1283 } | 1286 } |
| 1284 } | 1287 } |
| 1285 if (!Elements.isUnresolved(element) | 1288 if (!Elements.isUnresolved(element) |
| 1286 && element.kind == ElementKind.CLASS) { | 1289 && element.kind == ElementKind.CLASS) { |
| 1287 ClassElement classElement = element; | 1290 ClassElement classElement = element; |
| 1288 classElement.ensureResolved(compiler); | 1291 classElement.ensureResolved(compiler); |
| (...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1868 visit(link.head); | 1871 visit(link.head); |
| 1869 } | 1872 } |
| 1870 } | 1873 } |
| 1871 | 1874 |
| 1872 visitOperator(Operator node) { | 1875 visitOperator(Operator node) { |
| 1873 unimplemented(node, 'operator'); | 1876 unimplemented(node, 'operator'); |
| 1874 } | 1877 } |
| 1875 | 1878 |
| 1876 visitReturn(Return node) { | 1879 visitReturn(Return node) { |
| 1877 if (node.isRedirectingFactoryBody) { | 1880 if (node.isRedirectingFactoryBody) { |
| 1878 unimplemented(node, 'redirecting constructors'); | 1881 handleRedirectingFactoryBody(node); |
| 1882 } else { |
| 1883 visit(node.expression); |
| 1879 } | 1884 } |
| 1880 visit(node.expression); | 1885 } |
| 1886 |
| 1887 void handleRedirectingFactoryBody(Return node) { |
| 1888 Element redirectionTarget = resolveRedirectingFactory(node); |
| 1889 var type = mapping.getType(node.expression); |
| 1890 if (type is InterfaceType && !type.arguments.isEmpty) { |
| 1891 unimplemented(node.expression, 'type arguments on redirecting factory'); |
| 1892 } |
| 1893 useElement(node.expression, redirectionTarget); |
| 1894 assert(invariant(node, enclosingElement.isFactoryConstructor())); |
| 1895 FunctionElement constructor = enclosingElement; |
| 1896 if (constructor.modifiers.isConst() && |
| 1897 !redirectionTarget.modifiers.isConst()) { |
| 1898 error(node, MessageKind.CONSTRUCTOR_IS_NOT_CONST); |
| 1899 } |
| 1900 // TODO(ahe): Check that this doesn't lead to a cycle. For now, |
| 1901 // just make sure that the redirection target isn't itself a |
| 1902 // redirecting factory. |
| 1903 { // This entire block is temporary code per the above TODO. |
| 1904 FunctionElement targetImplementation = redirectionTarget.implementation; |
| 1905 FunctionExpression function = targetImplementation.parseNode(compiler); |
| 1906 if (function.body != null && function.body.asReturn() != null |
| 1907 && function.body.asReturn().isRedirectingFactoryBody) { |
| 1908 unimplemented(node.expression, 'redirecing to redirecting factory'); |
| 1909 } |
| 1910 } |
| 1911 constructor.defaultImplementation = redirectionTarget; |
| 1912 world.registerStaticUse(redirectionTarget); |
| 1881 } | 1913 } |
| 1882 | 1914 |
| 1883 visitThrow(Throw node) { | 1915 visitThrow(Throw node) { |
| 1884 if (!inCatchBlock && node.expression == null) { | 1916 if (!inCatchBlock && node.expression == null) { |
| 1885 error(node, MessageKind.THROW_WITHOUT_EXPRESSION); | 1917 error(node, MessageKind.THROW_WITHOUT_EXPRESSION); |
| 1886 } | 1918 } |
| 1887 visit(node.expression); | 1919 visit(node.expression); |
| 1888 } | 1920 } |
| 1889 | 1921 |
| 1890 visitVariableDefinitions(VariableDefinitions node) { | 1922 visitVariableDefinitions(VariableDefinitions node) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1944 | 1976 |
| 1945 /** | 1977 /** |
| 1946 * Try to resolve the constructor that is referred to by [node]. | 1978 * Try to resolve the constructor that is referred to by [node]. |
| 1947 * Note: this function may return an ErroneousFunctionElement instead of | 1979 * Note: this function may return an ErroneousFunctionElement instead of |
| 1948 * [null], if there is no corresponding constructor, class or library. | 1980 * [null], if there is no corresponding constructor, class or library. |
| 1949 */ | 1981 */ |
| 1950 FunctionElement resolveConstructor(NewExpression node) { | 1982 FunctionElement resolveConstructor(NewExpression node) { |
| 1951 return node.accept(new ConstructorResolver(compiler, this)); | 1983 return node.accept(new ConstructorResolver(compiler, this)); |
| 1952 } | 1984 } |
| 1953 | 1985 |
| 1986 FunctionElement resolveRedirectingFactory(Return node) { |
| 1987 return node.accept(new ConstructorResolver(compiler, this)); |
| 1988 } |
| 1989 |
| 1954 DartType resolveTypeRequired(TypeAnnotation node) { | 1990 DartType resolveTypeRequired(TypeAnnotation node) { |
| 1955 bool old = typeRequired; | 1991 bool old = typeRequired; |
| 1956 typeRequired = true; | 1992 typeRequired = true; |
| 1957 DartType result = resolveTypeAnnotation(node); | 1993 DartType result = resolveTypeAnnotation(node); |
| 1958 typeRequired = old; | 1994 typeRequired = old; |
| 1959 return result; | 1995 return result; |
| 1960 } | 1996 } |
| 1961 | 1997 |
| 1962 void analyzeTypeArgument(DartType annotation, DartType argument) { | 1998 void analyzeTypeArgument(DartType annotation, DartType argument) { |
| 1963 if (argument == null) return; | 1999 if (argument == null) return; |
| (...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2896 } | 2932 } |
| 2897 | 2933 |
| 2898 failOrReturnErroneousElement(Element enclosing, Node diagnosticNode, | 2934 failOrReturnErroneousElement(Element enclosing, Node diagnosticNode, |
| 2899 SourceString targetName, MessageKind kind, | 2935 SourceString targetName, MessageKind kind, |
| 2900 List arguments) { | 2936 List arguments) { |
| 2901 if (inConstContext) { | 2937 if (inConstContext) { |
| 2902 error(diagnosticNode, kind, arguments); | 2938 error(diagnosticNode, kind, arguments); |
| 2903 } else { | 2939 } else { |
| 2904 ResolutionWarning warning = new ResolutionWarning(kind, arguments); | 2940 ResolutionWarning warning = new ResolutionWarning(kind, arguments); |
| 2905 compiler.reportWarning(diagnosticNode, warning); | 2941 compiler.reportWarning(diagnosticNode, warning); |
| 2906 return new ErroneousFunctionElement(kind, arguments, targetName, | 2942 return new ErroneousElement(kind, arguments, targetName, enclosing); |
| 2907 enclosing); | |
| 2908 } | 2943 } |
| 2909 } | 2944 } |
| 2910 | 2945 |
| 2911 Selector createConstructorSelector(SourceString constructorName) { | 2946 Selector createConstructorSelector(SourceString constructorName) { |
| 2912 return constructorName == const SourceString('') | 2947 return constructorName == const SourceString('') |
| 2913 ? new Selector.callDefaultConstructor( | 2948 ? new Selector.callDefaultConstructor( |
| 2914 resolver.enclosingElement.getLibrary()) | 2949 resolver.enclosingElement.getLibrary()) |
| 2915 : new Selector.callConstructor( | 2950 : new Selector.callConstructor( |
| 2916 constructorName, | 2951 constructorName, |
| 2917 resolver.enclosingElement.getLibrary()); | 2952 resolver.enclosingElement.getLibrary()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 2938 } else if (inConstContext && !result.modifiers.isConst()) { | 2973 } else if (inConstContext && !result.modifiers.isConst()) { |
| 2939 error(diagnosticNode, MessageKind.CONSTRUCTOR_IS_NOT_CONST); | 2974 error(diagnosticNode, MessageKind.CONSTRUCTOR_IS_NOT_CONST); |
| 2940 } | 2975 } |
| 2941 return result; | 2976 return result; |
| 2942 } | 2977 } |
| 2943 | 2978 |
| 2944 visitNewExpression(NewExpression node) { | 2979 visitNewExpression(NewExpression node) { |
| 2945 inConstContext = node.isConst(); | 2980 inConstContext = node.isConst(); |
| 2946 Node selector = node.send.selector; | 2981 Node selector = node.send.selector; |
| 2947 Element e = visit(selector); | 2982 Element e = visit(selector); |
| 2948 if (!Elements.isUnresolved(e) && identical(e.kind, ElementKind.CLASS)) { | 2983 return finishConstructorReference(e, node.send.selector, node); |
| 2984 } |
| 2985 |
| 2986 /// Finishes resolution of a constructor reference and records the |
| 2987 /// type of the constructed instance on [expression]. |
| 2988 FunctionElement finishConstructorReference(Element e, |
| 2989 Node diagnosticNode, |
| 2990 Node expression) { |
| 2991 // Find the unnamed constructor if the reference resolved to a |
| 2992 // class. |
| 2993 if (!Elements.isUnresolved(e) && e.isClass()) { |
| 2949 ClassElement cls = e; | 2994 ClassElement cls = e; |
| 2950 cls.ensureResolved(compiler); | 2995 cls.ensureResolved(compiler); |
| 2951 if (cls.isInterface() && (cls.defaultClass == null)) { | 2996 if (cls.isInterface() && (cls.defaultClass == null)) { |
| 2952 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); | 2997 // TODO(ahe): Remove this check and error message when we |
| 2998 // don't have interfaces anymore. |
| 2999 error(diagnosticNode, |
| 3000 MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); |
| 2953 } | 3001 } |
| 2954 e = lookupConstructor(cls, selector, const SourceString('')); | 3002 // The unnamed constructor may not exist, so [e] may become unresolved. |
| 3003 e = lookupConstructor(cls, diagnosticNode, const SourceString('')); |
| 2955 } | 3004 } |
| 2956 if (type == null) { | 3005 if (type == null) { |
| 2957 if (Elements.isUnresolved(e)) { | 3006 if (Elements.isUnresolved(e)) { |
| 2958 type = compiler.dynamicClass.computeType(compiler); | 3007 type = compiler.dynamicClass.computeType(compiler); |
| 2959 } else { | 3008 } else { |
| 2960 type = e.getEnclosingClass().computeType(compiler).asRaw(); | 3009 type = e.getEnclosingClass().computeType(compiler).asRaw(); |
| 2961 } | 3010 } |
| 2962 } | 3011 } |
| 2963 resolver.mapping.setType(node, type); | 3012 resolver.mapping.setType(expression, type); |
| 2964 return e; | 3013 return e; |
| 2965 } | 3014 } |
| 2966 | 3015 |
| 2967 visitTypeAnnotation(TypeAnnotation node) { | 3016 visitTypeAnnotation(TypeAnnotation node) { |
| 2968 assert(invariant(node, type == null)); | 3017 assert(invariant(node, type == null)); |
| 2969 type = resolver.resolveTypeRequired(node); | 3018 type = resolver.resolveTypeRequired(node); |
| 2970 return resolver.mapping[node]; | 3019 return resolver.mapping[node]; |
| 2971 } | 3020 } |
| 2972 | 3021 |
| 2973 visitSend(Send node) { | 3022 visitSend(Send node) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 3001 return e; | 3050 return e; |
| 3002 } | 3051 } |
| 3003 | 3052 |
| 3004 Element visitIdentifier(Identifier node) { | 3053 Element visitIdentifier(Identifier node) { |
| 3005 SourceString name = node.source; | 3054 SourceString name = node.source; |
| 3006 Element e = resolver.lookup(node, name); | 3055 Element e = resolver.lookup(node, name); |
| 3007 // TODO(johnniwinther): Change errors to warnings, cf. 11.11.1. | 3056 // TODO(johnniwinther): Change errors to warnings, cf. 11.11.1. |
| 3008 if (e == null) { | 3057 if (e == null) { |
| 3009 return failOrReturnErroneousElement(resolver.enclosingElement, node, name, | 3058 return failOrReturnErroneousElement(resolver.enclosingElement, node, name, |
| 3010 MessageKind.CANNOT_RESOLVE, [name]); | 3059 MessageKind.CANNOT_RESOLVE, [name]); |
| 3060 } else if (e.isErroneous()) { |
| 3061 return e; |
| 3011 } else if (identical(e.kind, ElementKind.TYPEDEF)) { | 3062 } else if (identical(e.kind, ElementKind.TYPEDEF)) { |
| 3012 error(node, MessageKind.CANNOT_INSTANTIATE_TYPEDEF, [name]); | 3063 error(node, MessageKind.CANNOT_INSTANTIATE_TYPEDEF, [name]); |
| 3013 } else if (identical(e.kind, ElementKind.TYPE_VARIABLE)) { | 3064 } else if (identical(e.kind, ElementKind.TYPE_VARIABLE)) { |
| 3014 error(node, MessageKind.CANNOT_INSTANTIATE_TYPE_VARIABLE, [name]); | 3065 error(node, MessageKind.CANNOT_INSTANTIATE_TYPE_VARIABLE, [name]); |
| 3015 } else if (!identical(e.kind, ElementKind.CLASS) | 3066 } else if (!identical(e.kind, ElementKind.CLASS) |
| 3016 && !identical(e.kind, ElementKind.PREFIX)) { | 3067 && !identical(e.kind, ElementKind.PREFIX)) { |
| 3017 error(node, MessageKind.NOT_A_TYPE, [name]); | 3068 error(node, MessageKind.NOT_A_TYPE, [name]); |
| 3018 } | 3069 } |
| 3019 return e; | 3070 return e; |
| 3020 } | 3071 } |
| 3072 |
| 3073 /// Assumed to be called by [resolveRedirectingFactory]. |
| 3074 Element visitReturn(Return node) { |
| 3075 Node expression = node.expression; |
| 3076 return finishConstructorReference(visit(expression), |
| 3077 expression, expression); |
| 3078 } |
| 3021 } | 3079 } |
| OLD | NEW |