Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Unified Diff: sdk/lib/_internal/compiler/implementation/resolution/scope.dart

Issue 24488004: Implement correct scoping rules for variables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/compiler/implementation/resolution/scope.dart
diff --git a/sdk/lib/_internal/compiler/implementation/resolution/scope.dart b/sdk/lib/_internal/compiler/implementation/resolution/scope.dart
index 81eadaebbfd52814cf78aa4811fd73e8ee023b46..323d9523c9c8295d2d7199caa4a10da9303aa8f3 100644
--- a/sdk/lib/_internal/compiler/implementation/resolution/scope.dart
+++ b/sdk/lib/_internal/compiler/implementation/resolution/scope.dart
@@ -41,24 +41,6 @@ abstract class NestedScope extends Scope {
}
}
-class VariableDefinitionScope extends NestedScope {
- final SourceString variableName;
- bool variableReferencedInInitializer = false;
-
- VariableDefinitionScope(Scope parent, this.variableName) : super(parent);
-
- Element localLookup(SourceString name) {
- if (name == variableName) {
- variableReferencedInInitializer = true;
- }
- return null;
- }
-
- Element add(Element newElement) {
- throw "Cannot add element to VariableDefinitionScope";
- }
-}
-
/**
* [TypeDeclarationScope] defines the outer scope of a type declaration in
* which the declared type variables and the entities in the enclosing scope are
@@ -75,7 +57,7 @@ class TypeDeclarationScope extends NestedScope {
}
Element add(Element newElement) {
- throw "Cannot add element to TypeDeclarationScope";
+ throw "Cannot add element to TypeDeclarationScope($element)";
}
Element lookupTypeVariable(SourceString name) {
@@ -96,41 +78,33 @@ class TypeDeclarationScope extends NestedScope {
'TypeDeclarationScope($element)';
}
-abstract class MutableScope extends NestedScope {
+class BlockScope extends NestedScope {
final Map<SourceString, Element> elements;
- MutableScope(Scope parent)
- : super(parent),
- this.elements = new Map<SourceString, Element>() {
+ BlockScope(Scope parent)
+ : elements = <SourceString, Element>{}, super(parent) {
assert(parent != null);
}
- Element add(Element newElement) {
- if (elements.containsKey(newElement.name)) {
- return elements[newElement.name];
- }
- elements[newElement.name] = newElement;
- return newElement;
+ Element add(Element element) {
+ return elements.putIfAbsent(element.name, () => element);
}
Element localLookup(SourceString name) => elements[name];
+
+ String toString() => 'BlockScope(${elements.keys.toList()})';
}
-class MethodScope extends MutableScope {
+class MethodScope extends BlockScope {
final Element element;
- MethodScope(Scope parent, this.element)
- : super(parent);
+ MethodScope(Scope parent, this.element) : super(parent) {
+ assert(elements != null);
+ }
String toString() => 'MethodScope($element${elements.keys.toList()})';
}
-class BlockScope extends MutableScope {
- BlockScope(Scope parent) : super(parent);
-
- String toString() => 'BlockScope(${elements.keys.toList()})';
-}
-
/**
* [ClassScope] defines the inner scope of a class/interface declaration in
* which declared members, declared type variables, entities in the enclosing

Powered by Google App Engine
This is Rietveld 408576698