| 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 1908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1919 includeSuperMembers: true); | 1919 includeSuperMembers: true); |
| 1920 return null; | 1920 return null; |
| 1921 } | 1921 } |
| 1922 | 1922 |
| 1923 /** | 1923 /** |
| 1924 * Try to resolve the constructor that is referred to by [node]. | 1924 * Try to resolve the constructor that is referred to by [node]. |
| 1925 * Note: this function may return an ErroneousFunctionElement instead of | 1925 * Note: this function may return an ErroneousFunctionElement instead of |
| 1926 * [null], if there is no corresponding constructor, class or library. | 1926 * [null], if there is no corresponding constructor, class or library. |
| 1927 */ | 1927 */ |
| 1928 FunctionElement resolveConstructor(NewExpression node) { | 1928 FunctionElement resolveConstructor(NewExpression node) { |
| 1929 // Resolve the constructor that [node] refers to. | 1929 return node.accept(new ConstructorResolver(compiler, this)); |
| 1930 ConstructorResolver visitor = | |
| 1931 new ConstructorResolver(compiler, this, node.isConst()); | |
| 1932 FunctionElement constructor = node.accept(visitor); | |
| 1933 // Try to resolve the type that the new-expression constructs. | |
| 1934 TypeAnnotation annotation = node.send.getTypeAnnotation(); | |
| 1935 if (Elements.isUnresolved(constructor)) { | |
| 1936 // Resolve the type arguments. We cannot create a type and check the | |
| 1937 // number of type arguments for this annotation, because we do not know | |
| 1938 // the element. | |
| 1939 Link arguments = const Link<Node>(); | |
| 1940 if (annotation.typeArguments != null) { | |
| 1941 arguments = annotation.typeArguments.nodes; | |
| 1942 } | |
| 1943 for (Node argument in arguments) { | |
| 1944 resolveTypeRequired(argument); | |
| 1945 } | |
| 1946 } else { | |
| 1947 // Resolve and store the type this annotation resolves to. The type | |
| 1948 // is used in the backend, e.g., for creating runtime type information. | |
| 1949 // TODO(karlklose): This will resolve the class element again. Refactor | |
| 1950 // so we can use the TypeResolver. | |
| 1951 resolveTypeRequired(annotation); | |
| 1952 } | |
| 1953 return constructor; | |
| 1954 } | 1930 } |
| 1955 | 1931 |
| 1956 DartType resolveTypeRequired(TypeAnnotation node) { | 1932 DartType resolveTypeRequired(TypeAnnotation node) { |
| 1957 bool old = typeRequired; | 1933 bool old = typeRequired; |
| 1958 typeRequired = true; | 1934 typeRequired = true; |
| 1959 DartType result = resolveTypeAnnotation(node); | 1935 DartType result = resolveTypeAnnotation(node); |
| 1960 typeRequired = old; | 1936 typeRequired = old; |
| 1961 return result; | 1937 return result; |
| 1962 } | 1938 } |
| 1963 | 1939 |
| (...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2881 | 2857 |
| 2882 // TODO(ahe): This is temporary. | 2858 // TODO(ahe): This is temporary. |
| 2883 ClassElement get currentClass { | 2859 ClassElement get currentClass { |
| 2884 return enclosingElement.isMember() | 2860 return enclosingElement.isMember() |
| 2885 ? enclosingElement.getEnclosingClass() : null; | 2861 ? enclosingElement.getEnclosingClass() : null; |
| 2886 } | 2862 } |
| 2887 } | 2863 } |
| 2888 | 2864 |
| 2889 class ConstructorResolver extends CommonResolverVisitor<Element> { | 2865 class ConstructorResolver extends CommonResolverVisitor<Element> { |
| 2890 final ResolverVisitor resolver; | 2866 final ResolverVisitor resolver; |
| 2891 // TODO(ngeoffray): have this context at the call site. | 2867 bool inConstContext = false; |
| 2892 final bool inConstContext; | 2868 DartType type; |
| 2893 | 2869 |
| 2894 ConstructorResolver(Compiler compiler, | 2870 ConstructorResolver(Compiler compiler, this.resolver) : super(compiler); |
| 2895 this.resolver, | |
| 2896 this.inConstContext) | |
| 2897 : super(compiler); | |
| 2898 | 2871 |
| 2899 visitNode(Node node) { | 2872 visitNode(Node node) { |
| 2900 throw 'not supported'; | 2873 throw 'not supported'; |
| 2901 } | 2874 } |
| 2902 | 2875 |
| 2903 failOrReturnErroneousElement(Element enclosing, Node diagnosticNode, | 2876 failOrReturnErroneousElement(Element enclosing, Node diagnosticNode, |
| 2904 SourceString targetName, MessageKind kind, | 2877 SourceString targetName, MessageKind kind, |
| 2905 List arguments) { | 2878 List arguments) { |
| 2906 if (inConstContext) { | 2879 if (inConstContext) { |
| 2907 error(diagnosticNode, kind, arguments); | 2880 error(diagnosticNode, kind, arguments); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2940 new SourceString(fullConstructorName), | 2913 new SourceString(fullConstructorName), |
| 2941 MessageKind.CANNOT_FIND_CONSTRUCTOR, | 2914 MessageKind.CANNOT_FIND_CONSTRUCTOR, |
| 2942 [fullConstructorName]); | 2915 [fullConstructorName]); |
| 2943 } else if (inConstContext && !result.modifiers.isConst()) { | 2916 } else if (inConstContext && !result.modifiers.isConst()) { |
| 2944 error(diagnosticNode, MessageKind.CONSTRUCTOR_IS_NOT_CONST); | 2917 error(diagnosticNode, MessageKind.CONSTRUCTOR_IS_NOT_CONST); |
| 2945 } | 2918 } |
| 2946 return result; | 2919 return result; |
| 2947 } | 2920 } |
| 2948 | 2921 |
| 2949 visitNewExpression(NewExpression node) { | 2922 visitNewExpression(NewExpression node) { |
| 2923 inConstContext = node.isConst(); |
| 2950 Node selector = node.send.selector; | 2924 Node selector = node.send.selector; |
| 2951 Element e = visit(selector); | 2925 Element e = visit(selector); |
| 2952 if (!Elements.isUnresolved(e) && identical(e.kind, ElementKind.CLASS)) { | 2926 if (!Elements.isUnresolved(e) && identical(e.kind, ElementKind.CLASS)) { |
| 2953 ClassElement cls = e; | 2927 ClassElement cls = e; |
| 2954 cls.ensureResolved(compiler); | 2928 cls.ensureResolved(compiler); |
| 2955 if (cls.isInterface() && (cls.defaultClass == null)) { | 2929 if (cls.isInterface() && (cls.defaultClass == null)) { |
| 2956 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); | 2930 error(selector, MessageKind.CANNOT_INSTANTIATE_INTERFACE, [cls.name]); |
| 2957 } | 2931 } |
| 2958 e = lookupConstructor(cls, selector, const SourceString('')); | 2932 e = lookupConstructor(cls, selector, const SourceString('')); |
| 2959 } | 2933 } |
| 2934 if (type == null) { |
| 2935 if (Elements.isUnresolved(e)) { |
| 2936 type = compiler.dynamicClass.computeType(compiler); |
| 2937 } else { |
| 2938 type = e.getEnclosingClass().computeType(compiler).asRaw(); |
| 2939 } |
| 2940 } |
| 2941 resolver.mapping.setType(node, type); |
| 2960 return e; | 2942 return e; |
| 2961 } | 2943 } |
| 2962 | 2944 |
| 2963 visitTypeAnnotation(TypeAnnotation node) { | 2945 visitTypeAnnotation(TypeAnnotation node) { |
| 2964 return visit(node.typeName); | 2946 assert(invariant(node, type == null)); |
| 2947 type = resolver.resolveTypeRequired(node); |
| 2948 return resolver.mapping[node]; |
| 2965 } | 2949 } |
| 2966 | 2950 |
| 2967 visitSend(Send node) { | 2951 visitSend(Send node) { |
| 2968 Element e = visit(node.receiver); | 2952 Element e = visit(node.receiver); |
| 2969 if (Elements.isUnresolved(e)) return e; | 2953 if (Elements.isUnresolved(e)) return e; |
| 2970 Identifier name = node.selector.asIdentifier(); | 2954 Identifier name = node.selector.asIdentifier(); |
| 2971 if (name == null) internalError(node.selector, 'unexpected node'); | 2955 if (name == null) internalError(node.selector, 'unexpected node'); |
| 2972 | 2956 |
| 2973 if (identical(e.kind, ElementKind.CLASS)) { | 2957 if (identical(e.kind, ElementKind.CLASS)) { |
| 2974 ClassElement cls = e; | 2958 ClassElement cls = e; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3006 error(node, MessageKind.CANNOT_INSTANTIATE_TYPEDEF, [name]); | 2990 error(node, MessageKind.CANNOT_INSTANTIATE_TYPEDEF, [name]); |
| 3007 } else if (identical(e.kind, ElementKind.TYPE_VARIABLE)) { | 2991 } else if (identical(e.kind, ElementKind.TYPE_VARIABLE)) { |
| 3008 error(node, MessageKind.CANNOT_INSTANTIATE_TYPE_VARIABLE, [name]); | 2992 error(node, MessageKind.CANNOT_INSTANTIATE_TYPE_VARIABLE, [name]); |
| 3009 } else if (!identical(e.kind, ElementKind.CLASS) | 2993 } else if (!identical(e.kind, ElementKind.CLASS) |
| 3010 && !identical(e.kind, ElementKind.PREFIX)) { | 2994 && !identical(e.kind, ElementKind.PREFIX)) { |
| 3011 error(node, MessageKind.NOT_A_TYPE, [name]); | 2995 error(node, MessageKind.NOT_A_TYPE, [name]); |
| 3012 } | 2996 } |
| 3013 return e; | 2997 return e; |
| 3014 } | 2998 } |
| 3015 } | 2999 } |
| OLD | NEW |