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 library dart2js.resolution.scope; | 5 library dart2js.resolution.scope; |
6 | 6 |
7 import '../dart_types.dart'; | 7 import '../dart_types.dart'; |
8 import '../elements/elements.dart'; | 8 import '../elements/elements.dart'; |
9 | 9 |
10 abstract class Scope { | 10 abstract class Scope { |
11 /** | 11 /** |
12 * Adds [element] to this scope. This operation is only allowed on mutable | 12 * If an [Element] named `element.name` has already been added to this |
13 * scopes such as [MethodScope] and [BlockScope]. | 13 * [Scope], return that element and make no changes. If no such element has |
| 14 * been added, add the given [element] to this [Scope], and return [element]. |
| 15 * Note that this operation is only allowed on mutable scopes such as |
| 16 * [MethodScope] and [BlockScope]. |
14 */ | 17 */ |
15 Element add(Element element); | 18 Element add(Element element); |
16 | 19 |
17 /** | 20 /** |
18 * Looks up the [Element] for [name] in this scope. | 21 * Looks up the [Element] for [name] in this scope. |
19 */ | 22 */ |
20 Element lookup(String name); | 23 Element lookup(String name); |
21 | 24 |
22 static Scope buildEnclosingScope(Element element) { | 25 static Scope buildEnclosingScope(Element element) { |
23 return element.enclosingElement != null | 26 return element.enclosingElement != null |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 if (elements.containsKey(newElement.name)) { | 119 if (elements.containsKey(newElement.name)) { |
117 return elements[newElement.name]; | 120 return elements[newElement.name]; |
118 } | 121 } |
119 elements[newElement.name] = newElement; | 122 elements[newElement.name] = newElement; |
120 return newElement; | 123 return newElement; |
121 } | 124 } |
122 | 125 |
123 Element localLookup(String name) => elements[name]; | 126 Element localLookup(String name) => elements[name]; |
124 } | 127 } |
125 | 128 |
| 129 /** |
| 130 * [ExtensionScope] enables the creation of an extended version of an |
| 131 * existing [NestedScope], received during construction and stored in |
| 132 * [extendee]. An [ExtensionScope] will treat an added `element` as conflicting |
| 133 * if an element `e` where `e.name == element.name` exists among the elements |
| 134 * added to this [ExtensionScope], or among the ones added to [extendee] |
| 135 * (according to `extendee.localLookup`). In this sense, it represents the |
| 136 * union of the bindings stored locally in [elements] and the bindings in |
| 137 * [extendee], not a new scope which is nested inside [extendee]. |
| 138 * |
| 139 * Note that it is required that no bindings are added to [extendee] during the |
| 140 * lifetime of this [ExtensionScope]: That would enable duplicates to be |
| 141 * introduced into the extended scope consisting of [this] plus [extendee] |
| 142 * without detection. |
| 143 */ |
| 144 class ExtensionScope extends Scope { |
| 145 final NestedScope extendee; |
| 146 final Map<String, Element> elements; |
| 147 |
| 148 ExtensionScope(this.extendee) : this.elements = new Map<String, Element>() { |
| 149 assert(extendee != null); |
| 150 } |
| 151 |
| 152 Element lookup(String name) { |
| 153 Element result = elements[name]; |
| 154 if (result != null) return result; |
| 155 return extendee.lookup(name); |
| 156 } |
| 157 |
| 158 Element add(Element newElement) { |
| 159 if (elements.containsKey(newElement.name)) { |
| 160 return elements[newElement.name]; |
| 161 } |
| 162 Element existing = extendee.localLookup(newElement.name); |
| 163 if (existing != null) return existing; |
| 164 elements[newElement.name] = newElement; |
| 165 return newElement; |
| 166 } |
| 167 } |
| 168 |
126 class MethodScope extends MutableScope { | 169 class MethodScope extends MutableScope { |
127 final Element element; | 170 final Element element; |
128 | 171 |
129 MethodScope(Scope parent, this.element) : super(parent); | 172 MethodScope(Scope parent, this.element) : super(parent); |
130 | 173 |
131 String toString() => 'MethodScope($element${elements.keys.toList()})'; | 174 String toString() => 'MethodScope($element${elements.keys.toList()})'; |
132 } | 175 } |
133 | 176 |
134 class BlockScope extends MutableScope { | 177 class BlockScope extends MutableScope { |
135 BlockScope(Scope parent) : super(parent); | 178 BlockScope(Scope parent) : super(parent); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 221 |
179 Element localLookup(String name) => library.find(name); | 222 Element localLookup(String name) => library.find(name); |
180 Element lookup(String name) => localLookup(name); | 223 Element lookup(String name) => localLookup(name); |
181 | 224 |
182 Element add(Element newElement) { | 225 Element add(Element newElement) { |
183 throw "Cannot add an element to a library scope"; | 226 throw "Cannot add an element to a library scope"; |
184 } | 227 } |
185 | 228 |
186 String toString() => 'LibraryScope($library)'; | 229 String toString() => 'LibraryScope($library)'; |
187 } | 230 } |
OLD | NEW |