| 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 abstract class TreeElements { | 5 abstract class TreeElements { |
| 6 Element operator[](Node node); | 6 Element operator[](Node node); |
| 7 Selector getSelector(Send send); | 7 Selector getSelector(Send send); |
| 8 DartType getType(TypeAnnotation annotation); | 8 DartType getType(TypeAnnotation annotation); |
| 9 bool isParameterChecked(Element element); | 9 bool isParameterChecked(Element element); |
| 10 } | 10 } |
| (...skipping 1329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1340 | 1340 |
| 1341 DartType useType(TypeAnnotation annotation, DartType type) { | 1341 DartType useType(TypeAnnotation annotation, DartType type) { |
| 1342 if (type != null) { | 1342 if (type != null) { |
| 1343 mapping.setType(annotation, type); | 1343 mapping.setType(annotation, type); |
| 1344 useElement(annotation, type.element); | 1344 useElement(annotation, type.element); |
| 1345 } | 1345 } |
| 1346 return type; | 1346 return type; |
| 1347 } | 1347 } |
| 1348 | 1348 |
| 1349 void setupFunction(FunctionExpression node, FunctionElement function) { | 1349 void setupFunction(FunctionExpression node, FunctionElement function) { |
| 1350 // If [function] is the [enclosingElement], the [scope] has | 1350 scope = new MethodScope(scope, function); |
| 1351 // already been set in the constructor of [ResolverVisitor]. | |
| 1352 if (function != enclosingElement) scope = new MethodScope(scope, function); | |
| 1353 | 1351 |
| 1354 // Put the parameters in scope. | 1352 // Put the parameters in scope. |
| 1355 FunctionSignature functionParameters = | 1353 FunctionSignature functionParameters = |
| 1356 function.computeSignature(compiler); | 1354 function.computeSignature(compiler); |
| 1357 Link<Node> parameterNodes = (node.parameters == null) | 1355 Link<Node> parameterNodes = (node.parameters == null) |
| 1358 ? const Link<Node>() : node.parameters.nodes; | 1356 ? const Link<Node>() : node.parameters.nodes; |
| 1359 functionParameters.forEachParameter((Element element) { | 1357 functionParameters.forEachParameter((Element element) { |
| 1360 if (element == functionParameters.optionalParameters.head) { | 1358 if (element == functionParameters.optionalParameters.head) { |
| 1361 NodeList nodes = parameterNodes.head; | 1359 NodeList nodes = parameterNodes.head; |
| 1362 parameterNodes = nodes.nodes; | 1360 parameterNodes = nodes.nodes; |
| (...skipping 1582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2945 } | 2943 } |
| 2946 } else { | 2944 } else { |
| 2947 internalError(node.receiver, 'unexpected element $e'); | 2945 internalError(node.receiver, 'unexpected element $e'); |
| 2948 } | 2946 } |
| 2949 return e; | 2947 return e; |
| 2950 } | 2948 } |
| 2951 | 2949 |
| 2952 Element visitIdentifier(Identifier node) { | 2950 Element visitIdentifier(Identifier node) { |
| 2953 SourceString name = node.source; | 2951 SourceString name = node.source; |
| 2954 Element e = resolver.lookup(node, name); | 2952 Element e = resolver.lookup(node, name); |
| 2953 // TODO(johnniwinther): Change errors to warnings, cf. 11.11.1. |
| 2955 if (e == null) { | 2954 if (e == null) { |
| 2956 return failOrReturnErroneousElement(resolver.enclosingElement, node, name, | 2955 return failOrReturnErroneousElement(resolver.enclosingElement, node, name, |
| 2957 MessageKind.CANNOT_RESOLVE, [name]); | 2956 MessageKind.CANNOT_RESOLVE, [name]); |
| 2958 } else if (identical(e.kind, ElementKind.TYPEDEF)) { | 2957 } else if (identical(e.kind, ElementKind.TYPEDEF)) { |
| 2959 error(node, MessageKind.CANNOT_INSTANTIATE_TYPEDEF, [name]); | 2958 error(node, MessageKind.CANNOT_INSTANTIATE_TYPEDEF, [name]); |
| 2959 } else if (identical(e.kind, ElementKind.TYPE_VARIABLE)) { |
| 2960 error(node, MessageKind.CANNOT_INSTANTIATE_TYPE_VARIABLE, [name]); |
| 2960 } else if (!identical(e.kind, ElementKind.CLASS) | 2961 } else if (!identical(e.kind, ElementKind.CLASS) |
| 2961 && !identical(e.kind, ElementKind.PREFIX)) { | 2962 && !identical(e.kind, ElementKind.PREFIX)) { |
| 2962 error(node, MessageKind.NOT_A_TYPE, [name]); | 2963 error(node, MessageKind.NOT_A_TYPE, [name]); |
| 2963 } | 2964 } |
| 2964 return e; | 2965 return e; |
| 2965 } | 2966 } |
| 2966 } | 2967 } |
| 2967 | 2968 |
| 2968 abstract class Scope { | 2969 abstract class Scope { |
| 2969 final Element element; | 2970 final Element element; |
| 2970 final Scope parent; | 2971 final Scope parent; |
| 2971 | 2972 |
| 2972 Scope(this.parent, this.element); | 2973 Scope(this.parent, this.element); |
| 2973 abstract Element add(Element element); | 2974 abstract Element add(Element element); |
| 2974 | 2975 |
| 2975 Element lookup(SourceString name) { | 2976 Element lookup(SourceString name) { |
| 2976 Element result = localLookup(name); | 2977 Element result = localLookup(name); |
| 2977 if (result != null) return result; | 2978 if (result != null) return result; |
| 2978 return parent.lookup(name); | 2979 return parent.lookup(name); |
| 2979 } | 2980 } |
| 2980 | 2981 |
| 2981 Element lexicalLookup(SourceString name) { | |
| 2982 Element result = localLookup(name); | |
| 2983 if (result != null) return result; | |
| 2984 return parent.lexicalLookup(name); | |
| 2985 } | |
| 2986 | |
| 2987 abstract Element localLookup(SourceString name); | 2982 abstract Element localLookup(SourceString name); |
| 2988 } | 2983 } |
| 2989 | 2984 |
| 2990 class VariableScope extends Scope { | |
| 2991 VariableScope(parent, element) : super(parent, element); | |
| 2992 | |
| 2993 Element add(Element newElement) { | |
| 2994 throw "Cannot add element to VariableScope"; | |
| 2995 } | |
| 2996 | |
| 2997 Element localLookup(SourceString name) => null; | |
| 2998 | |
| 2999 String toString() => '$element > $parent'; | |
| 3000 } | |
| 3001 | |
| 3002 /** | 2985 /** |
| 3003 * [TypeDeclarationScope] defines the outer scope of a type declaration in | 2986 * [TypeDeclarationScope] defines the outer scope of a type declaration in |
| 3004 * which the declared type variables and the entities in the enclosing scope are | 2987 * which the declared type variables and the entities in the enclosing scope are |
| 3005 * available but where declared and inherited members are not available. This | 2988 * available but where declared and inherited members are not available. This |
| 3006 * scope is only used for class/interface declarations during resolution of the | 2989 * scope is only used for class/interface declarations during resolution of the |
| 3007 * class hierarchy. In all other cases [ClassScope] is used. | 2990 * class hierarchy. In all other cases [ClassScope] is used. |
| 3008 */ | 2991 */ |
| 3009 class TypeDeclarationScope extends Scope { | 2992 class TypeDeclarationScope extends Scope { |
| 3010 TypeDeclarationElement get element => super.element; | 2993 TypeDeclarationElement get element => super.element; |
| 3011 | 2994 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3061 | 3044 |
| 3062 String toString() => 'block${elements.getKeys()}'; | 3045 String toString() => 'block${elements.getKeys()}'; |
| 3063 } | 3046 } |
| 3064 | 3047 |
| 3065 /** | 3048 /** |
| 3066 * [ClassScope] defines the inner scope of a class/interface declaration in | 3049 * [ClassScope] defines the inner scope of a class/interface declaration in |
| 3067 * which declared members, declared type variables, entities in the enclosing | 3050 * which declared members, declared type variables, entities in the enclosing |
| 3068 * scope and inherited members are available, in the given order. | 3051 * scope and inherited members are available, in the given order. |
| 3069 */ | 3052 */ |
| 3070 class ClassScope extends TypeDeclarationScope { | 3053 class ClassScope extends TypeDeclarationScope { |
| 3071 bool inStaticContext = false; | 3054 ClassElement get element => super.element; |
| 3072 | 3055 |
| 3073 ClassScope(Scope parentScope, ClassElement element) | 3056 ClassScope(Scope parentScope, ClassElement element) |
| 3074 : super(parentScope, element) { | 3057 : super(parentScope, element) { |
| 3075 assert(parent != null); | 3058 assert(parent != null); |
| 3076 } | 3059 } |
| 3077 | 3060 |
| 3078 Element localLookup(SourceString name) { | 3061 Element localLookup(SourceString name) { |
| 3079 ClassElement cls = element; | 3062 Element result = element.lookupLocalMember(name); |
| 3080 Element result = cls.lookupLocalMember(name); | |
| 3081 if (result != null) return result; | 3063 if (result != null) return result; |
| 3082 if (!inStaticContext) { | 3064 return super.localLookup(name); |
| 3083 // If not in a static context, we can lookup in the | |
| 3084 // TypeDeclaration scope, which contains the type variables of | |
| 3085 // the class. | |
| 3086 result = super.localLookup(name); | |
| 3087 } | |
| 3088 return result; | |
| 3089 } | 3065 } |
| 3090 | 3066 |
| 3091 Element lookup(SourceString name) { | 3067 Element lookup(SourceString name) { |
| 3092 Element result = localLookup(name); | 3068 Element result = localLookup(name); |
| 3093 if (result != null) return result; | 3069 if (result != null) return result; |
| 3094 result = parent.lookup(name); | 3070 result = parent.lookup(name); |
| 3095 if (result != null) return result; | 3071 if (result != null) return result; |
| 3096 ClassElement cls = element; | 3072 return element.lookupSuperMember(name); |
| 3097 return cls.lookupSuperMember(name); | |
| 3098 } | 3073 } |
| 3099 | 3074 |
| 3100 Element add(Element newElement) { | 3075 Element add(Element newElement) { |
| 3101 throw "Cannot add an element in a class scope"; | 3076 throw "Cannot add an element in a class scope"; |
| 3102 } | 3077 } |
| 3103 | 3078 |
| 3104 String toString() => 'ClassScope($element)'; | 3079 String toString() => 'ClassScope($element)'; |
| 3105 } | 3080 } |
| 3106 | 3081 |
| 3107 // TODO(johnniwinther): Refactor scopes to avoid class explosion. | 3082 // TODO(johnniwinther): Refactor scopes to avoid class explosion. |
| 3108 class PatchClassScope extends TypeDeclarationScope { | 3083 class PatchClassScope extends TypeDeclarationScope { |
| 3109 bool inStaticContext = false; | |
| 3110 ClassElement get origin => element; | 3084 ClassElement get origin => element; |
| 3111 final ClassElement patch; | 3085 final ClassElement patch; |
| 3112 | 3086 |
| 3113 PatchClassScope(Scope parentScope, | 3087 PatchClassScope(Scope parentScope, |
| 3114 ClassElement origin, ClassElement this.patch) | 3088 ClassElement origin, ClassElement this.patch) |
| 3115 : super(parentScope, origin) { | 3089 : super(parentScope, origin) { |
| 3116 assert(parent != null); | 3090 assert(parent != null); |
| 3117 } | 3091 } |
| 3118 | 3092 |
| 3119 Element localLookup(SourceString name) { | 3093 Element localLookup(SourceString name) { |
| 3120 Element result = patch.lookupLocalMember(name); | 3094 Element result = patch.lookupLocalMember(name); |
| 3121 if (result != null) return result; | 3095 if (result != null) return result; |
| 3122 result = origin.lookupLocalMember(name); | 3096 result = origin.lookupLocalMember(name); |
| 3123 if (result != null) return result; | 3097 if (result != null) return result; |
| 3124 if (!inStaticContext) { | 3098 result = super.localLookup(name); |
| 3125 // If not in a static context, we can lookup in the | 3099 if (result != null) return result; |
| 3126 // TypeDeclaration scope, which contains the type variables of | |
| 3127 // the class. | |
| 3128 result = super.localLookup(name); | |
| 3129 if (result != null) return result; | |
| 3130 } | |
| 3131 result = parent.lookup(name); | 3100 result = parent.lookup(name); |
| 3132 if (result != null) return result; | 3101 if (result != null) return result; |
| 3133 return result; | 3102 return result; |
| 3134 } | 3103 } |
| 3135 | 3104 |
| 3136 Element lookup(SourceString name) { | 3105 Element lookup(SourceString name) { |
| 3137 Element result = localLookup(name); | 3106 Element result = localLookup(name); |
| 3138 if (result != null) return result; | 3107 if (result != null) return result; |
| 3139 // TODO(johnniwinther): Should we support patch lookup on supertypes? | 3108 // TODO(johnniwinther): Should we support patch lookup on supertypes? |
| 3140 return origin.lookupSuperMember(name); | 3109 return origin.lookupSuperMember(name); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3222 return result; | 3191 return result; |
| 3223 } | 3192 } |
| 3224 Element lookup(SourceString name) => localLookup(name); | 3193 Element lookup(SourceString name) => localLookup(name); |
| 3225 Element lexicalLookup(SourceString name) => localLookup(name); | 3194 Element lexicalLookup(SourceString name) => localLookup(name); |
| 3226 | 3195 |
| 3227 Element add(Element newElement) { | 3196 Element add(Element newElement) { |
| 3228 throw "Cannot add an element in a patch library scope"; | 3197 throw "Cannot add an element in a patch library scope"; |
| 3229 } | 3198 } |
| 3230 String toString() => 'PatchLibraryScope($origin,$patch)'; | 3199 String toString() => 'PatchLibraryScope($origin,$patch)'; |
| 3231 } | 3200 } |
| OLD | NEW |