| 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 package com.google.dart.compiler.resolver; | 5 package com.google.dart.compiler.resolver; |
| 6 | 6 |
| 7 import com.google.common.annotations.VisibleForTesting; | 7 import com.google.common.annotations.VisibleForTesting; |
| 8 import com.google.common.collect.Lists; | 8 import com.google.common.collect.Lists; |
| 9 import com.google.common.collect.Sets; | 9 import com.google.common.collect.Sets; |
| 10 import com.google.dart.compiler.DartCompilationPhase; | 10 import com.google.dart.compiler.DartCompilationPhase; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 * - A local variable shadowing a class member. | 177 * - A local variable shadowing a class member. |
| 178 * - Using 'this' or 'super' in a static or factory method, or in an initiali
zer. | 178 * - Using 'this' or 'super' in a static or factory method, or in an initiali
zer. |
| 179 * - Using 'super' in a class without a super class. | 179 * - Using 'super' in a class without a super class. |
| 180 * - Incorrectly using a resolved element. | 180 * - Incorrectly using a resolved element. |
| 181 */ | 181 */ |
| 182 @VisibleForTesting | 182 @VisibleForTesting |
| 183 public class ResolveElementsVisitor extends ResolveVisitor { | 183 public class ResolveElementsVisitor extends ResolveVisitor { |
| 184 private EnclosingElement currentHolder; | 184 private EnclosingElement currentHolder; |
| 185 private EnclosingElement enclosingElement; | 185 private EnclosingElement enclosingElement; |
| 186 private MethodElement currentMethod; | 186 private MethodElement currentMethod; |
| 187 private boolean inInstanceVariableInitializer; |
| 187 private boolean inInitializer; | 188 private boolean inInitializer; |
| 188 private MethodElement innermostFunction; | 189 private MethodElement innermostFunction; |
| 189 private ResolutionContext context; | 190 private ResolutionContext context; |
| 190 private Set<LabelElement> referencedLabels = Sets.newHashSet(); | 191 private Set<LabelElement> referencedLabels = Sets.newHashSet(); |
| 191 private Set<LabelElement> labelsInScopes = Sets.newHashSet(); | 192 private Set<LabelElement> labelsInScopes = Sets.newHashSet(); |
| 192 private Set<FieldElement> finalsNeedingInitializing = Sets.newHashSet(); | 193 private Set<FieldElement> finalsNeedingInitializing = Sets.newHashSet(); |
| 193 | 194 |
| 194 @VisibleForTesting | 195 @VisibleForTesting |
| 195 public ResolveElementsVisitor(ResolutionContext context, | 196 public ResolveElementsVisitor(ResolutionContext context, |
| 196 EnclosingElement currentHolder, | 197 EnclosingElement currentHolder, |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 | 656 |
| 656 @Override | 657 @Override |
| 657 public Element visitField(DartField node) { | 658 public Element visitField(DartField node) { |
| 658 DartExpression expression = node.getValue(); | 659 DartExpression expression = node.getValue(); |
| 659 Modifiers modifiers = node.getModifiers(); | 660 Modifiers modifiers = node.getModifiers(); |
| 660 boolean isFinal = modifiers.isFinal(); | 661 boolean isFinal = modifiers.isFinal(); |
| 661 boolean isTopLevel = ElementKind.of(currentHolder).equals(ElementKind.LIBR
ARY); | 662 boolean isTopLevel = ElementKind.of(currentHolder).equals(ElementKind.LIBR
ARY); |
| 662 boolean isStatic = modifiers.isStatic(); | 663 boolean isStatic = modifiers.isStatic(); |
| 663 | 664 |
| 664 if (expression != null) { | 665 if (expression != null) { |
| 665 resolve(expression); | 666 inInstanceVariableInitializer = !isTopLevel; |
| 667 try { |
| 668 resolve(expression); |
| 669 } finally { |
| 670 inInstanceVariableInitializer = false; |
| 671 } |
| 666 // Now, this constant has a type. Save it for future reference. | 672 // Now, this constant has a type. Save it for future reference. |
| 667 Element element = node.getElement(); | 673 Element element = node.getElement(); |
| 668 Type expressionType = expression.getType(); | 674 Type expressionType = expression.getType(); |
| 669 if (isFinal && expressionType != null && TypeKind.of(element.getType())
== TypeKind.DYNAMIC) { | 675 if (isFinal && expressionType != null && TypeKind.of(element.getType())
== TypeKind.DYNAMIC) { |
| 670 Type fieldType = Types.makeInferred(expressionType); | 676 Type fieldType = Types.makeInferred(expressionType); |
| 671 Elements.setType(element, fieldType); | 677 Elements.setType(element, fieldType); |
| 672 } | 678 } |
| 673 } else if (isFinal) { | 679 } else if (isFinal) { |
| 674 if (modifiers.isConstant()) { | 680 if (modifiers.isConstant()) { |
| 675 onError(node, ResolverErrorCode.CONST_REQUIRES_VALUE); | 681 onError(node, ResolverErrorCode.CONST_REQUIRES_VALUE); |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 return recordElement(x, element); | 1094 return recordElement(x, element); |
| 1089 } | 1095 } |
| 1090 | 1096 |
| 1091 /** | 1097 /** |
| 1092 * Possibly recursive check on the resolved identifier. | 1098 * Possibly recursive check on the resolved identifier. |
| 1093 */ | 1099 */ |
| 1094 private Element checkResolvedIdentifier(DartIdentifier x, boolean isQualifie
r, Scope scope, | 1100 private Element checkResolvedIdentifier(DartIdentifier x, boolean isQualifie
r, Scope scope, |
| 1095 String name, Element element) { | 1101 String name, Element element) { |
| 1096 switch (element.getKind()) { | 1102 switch (element.getKind()) { |
| 1097 case FIELD: | 1103 case FIELD: |
| 1098 if (inStaticContext(currentMethod) && !inStaticContext(element)) { | 1104 if (!inStaticContext(element)) { |
| 1099 onError(x, ResolverErrorCode.ILLEGAL_FIELD_ACCESS_FROM_STATIC, | 1105 if (inInstanceVariableInitializer) { |
| 1100 name); | 1106 onError(x, ResolverErrorCode.CANNOT_USE_INSTANCE_FIELD_IN_INSTANCE
_FIELD_INITIALIZER); |
| 1107 } else if (inStaticContext(currentMethod)) { |
| 1108 onError(x, ResolverErrorCode.ILLEGAL_FIELD_ACCESS_FROM_STATIC, nam
e); |
| 1109 } |
| 1101 } | 1110 } |
| 1102 if (isIllegalPrivateAccess(x, enclosingElement, element, x.getName()))
{ | 1111 if (isIllegalPrivateAccess(x, enclosingElement, element, x.getName()))
{ |
| 1103 return null; | 1112 return null; |
| 1104 } | 1113 } |
| 1105 break; | 1114 break; |
| 1106 case METHOD: | 1115 case METHOD: |
| 1107 if (inStaticContext(currentMethod) && !inStaticContext(element)) { | 1116 if (inStaticContext(currentMethod) && !inStaticContext(element)) { |
| 1108 onError(x, ResolverErrorCode.ILLEGAL_METHOD_ACCESS_FROM_STATIC, | 1117 onError(x, ResolverErrorCode.ILLEGAL_METHOD_ACCESS_FROM_STATIC, |
| 1109 name); | 1118 name); |
| 1110 } | 1119 } |
| (...skipping 1033 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2144 } | 2153 } |
| 2145 return false; | 2154 return false; |
| 2146 } | 2155 } |
| 2147 | 2156 |
| 2148 @Override | 2157 @Override |
| 2149 boolean isStaticContext() { | 2158 boolean isStaticContext() { |
| 2150 return inStaticContext(currentMethod); | 2159 return inStaticContext(currentMethod); |
| 2151 } | 2160 } |
| 2152 | 2161 |
| 2153 private boolean inStaticContext(Element element) { | 2162 private boolean inStaticContext(Element element) { |
| 2154 return element == null || Elements.isTopLevel(element) | 2163 return element == null || Elements.isTopLevel(element) || element.getModif
iers().isStatic() |
| 2155 || element.getModifiers().isStatic() || element.getModifiers().isFacto
ry(); | 2164 || element.getModifiers().isConstant() || element.getModifiers().isFac
tory(); |
| 2156 } | 2165 } |
| 2157 | 2166 |
| 2158 @Override | 2167 @Override |
| 2159 boolean isFactoryContext() { | 2168 boolean isFactoryContext() { |
| 2160 return inFactoryContext(currentMethod); | 2169 return inFactoryContext(currentMethod); |
| 2161 } | 2170 } |
| 2162 | 2171 |
| 2163 boolean isStaticContextOrInitializer() { | 2172 boolean isStaticContextOrInitializer() { |
| 2164 return inStaticContext(currentMethod) || inInitializer; | 2173 return inStaticContext(currentMethod) || inInitializer; |
| 2165 } | 2174 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2218 ClassElement currentClass = (ClassElement) constructor.getEnclosingEle
ment(); | 2227 ClassElement currentClass = (ClassElement) constructor.getEnclosingEle
ment(); |
| 2219 if (nextClass == currentClass) { | 2228 if (nextClass == currentClass) { |
| 2220 return (ConstructorNodeElement) nextConstructorElement; | 2229 return (ConstructorNodeElement) nextConstructorElement; |
| 2221 } | 2230 } |
| 2222 } | 2231 } |
| 2223 } | 2232 } |
| 2224 } | 2233 } |
| 2225 return null; | 2234 return null; |
| 2226 } | 2235 } |
| 2227 } | 2236 } |
| OLD | NEW |