OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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.dart.compiler.DartCompilationError; | 8 import com.google.dart.compiler.DartCompilationError; |
9 import com.google.dart.compiler.DartCompilerContext; | 9 import com.google.dart.compiler.DartCompilerContext; |
10 import com.google.dart.compiler.ErrorCode; | 10 import com.google.dart.compiler.ErrorCode; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 } | 59 } |
60 | 60 |
61 ResolutionContext extend(String name) { | 61 ResolutionContext extend(String name) { |
62 return new ResolutionContext(new Scope(name, scope.getLibrary(), scope), con
text, typeProvider); | 62 return new ResolutionContext(new Scope(name, scope.getLibrary(), scope), con
text, typeProvider); |
63 } | 63 } |
64 | 64 |
65 Scope getScope() { | 65 Scope getScope() { |
66 return scope; | 66 return scope; |
67 } | 67 } |
68 | 68 |
69 void declare(Element element) { | 69 void declare(Element element, ErrorCode errorCode, ErrorCode warningCode) { |
70 Element existingElement = scope.declareElement(element.getName(), element); | 70 String name = element.getName(); |
71 if (existingElement != null) { | 71 Element existingLocalElement = scope.findLocalElement(name); |
72 onError(element.getNode(), ResolverErrorCode.DUPLICATE_TOP_LEVEL_DEFINITIO
N, | 72 // Check for duplicate declaration in the enclosing scope. |
73 element.getName()); | 73 if (existingLocalElement == null && warningCode != null) { |
| 74 Element existingElement = scope.findElement(scope.getLibrary(), name); |
| 75 if (existingElement != null) { |
| 76 if (!Elements.isConstructorParameter(element) |
| 77 && !Elements.isParameterOfMethodWithoutBody(element) |
| 78 && !(Elements.isStaticContext(element) && !Elements.isStaticContext(
existingElement)) |
| 79 && !existingElement.getModifiers().isAbstractField()) { |
| 80 DartNode nameNode = Elements.getNameNode(element); |
| 81 String existingLocation = Elements.getRelativeElementLocation(element,
existingElement); |
| 82 onError(nameNode, warningCode, name, existingElement, existingLocation
); |
| 83 } |
| 84 } |
74 } | 85 } |
| 86 // Check for duplicate declaration in the same scope. |
| 87 if (existingLocalElement != null && errorCode != null) { |
| 88 DartNode nameNode = Elements.getNameNode(element); |
| 89 String existingLocation = Elements.getRelativeElementLocation(element, exi
stingLocalElement); |
| 90 onError(nameNode, errorCode, name, existingLocation); |
| 91 } |
| 92 // Declare, may be hide existing element. |
| 93 scope.declareElement(name, element); |
75 } | 94 } |
76 | 95 |
77 void pushScope(String name) { | 96 void pushScope(String name) { |
78 scope = new Scope(name, scope.getLibrary(), scope); | 97 scope = new Scope(name, scope.getLibrary(), scope); |
79 } | 98 } |
80 | 99 |
81 void popScope() { | 100 void popScope() { |
82 scope = scope.getParent(); | 101 scope = scope.getParent(); |
83 } | 102 } |
84 | 103 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 return element.getTypeVariable(); | 257 return element.getTypeVariable(); |
239 } | 258 } |
240 | 259 |
241 Element resolveName(DartNode node) { | 260 Element resolveName(DartNode node) { |
242 return node.accept(new Selector()); | 261 return node.accept(new Selector()); |
243 } | 262 } |
244 | 263 |
245 MethodElement declareFunction(DartFunctionExpression node) { | 264 MethodElement declareFunction(DartFunctionExpression node) { |
246 MethodElement element = Elements.methodFromFunctionExpression(node, Modifier
s.NONE); | 265 MethodElement element = Elements.methodFromFunctionExpression(node, Modifier
s.NONE); |
247 if (node.getFunctionName() != null) { | 266 if (node.getFunctionName() != null) { |
248 declare(element); | 267 declare( |
| 268 element, |
| 269 ResolverErrorCode.DUPLICATE_FUNCTION_EXPRESSION, |
| 270 ResolverErrorCode.DUPLICATE_FUNCTION_EXPRESSION_WARNING); |
249 } | 271 } |
250 return element; | 272 return element; |
251 } | 273 } |
252 | 274 |
253 void pushFunctionScope(DartFunctionExpression x) { | 275 void pushFunctionScope(DartFunctionExpression x) { |
254 pushScope(x.getFunctionName() == null ? "<function>" : x.getFunctionName()); | 276 pushScope(x.getFunctionName() == null ? "<function>" : x.getFunctionName()); |
255 } | 277 } |
256 | 278 |
257 void pushFunctionAliasScope(DartFunctionTypeAlias x) { | 279 void pushFunctionAliasScope(DartFunctionTypeAlias x) { |
258 pushScope(x.getName().getTargetName() == null ? "<function>" : x.getName().g
etTargetName()); | 280 pushScope(x.getName().getTargetName() == null ? "<function>" : x.getName().g
etTargetName()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 return null; | 317 return null; |
296 } | 318 } |
297 | 319 |
298 @Override | 320 @Override |
299 public Element visitIdentifier(DartIdentifier node) { | 321 public Element visitIdentifier(DartIdentifier node) { |
300 String name = node.getTargetName(); | 322 String name = node.getTargetName(); |
301 return scope.findElement(scope.getLibrary(), name); | 323 return scope.findElement(scope.getLibrary(), name); |
302 } | 324 } |
303 } | 325 } |
304 } | 326 } |
OLD | NEW |