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