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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 String toString() => 'ClassScope($element)'; | 214 String toString() => 'ClassScope($element)'; |
172 } | 215 } |
173 | 216 |
174 class LibraryScope implements Scope { | 217 class LibraryScope implements Scope { |
175 final LibraryElement library; | 218 final LibraryElement library; |
176 | 219 |
177 LibraryScope(LibraryElement this.library); | 220 LibraryScope(LibraryElement this.library); |
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); |
224 bool isDuplicate(Element element) => element == localLookup(element.name); | |
Johnni Winther
2016/06/29 07:22:24
Remove this.
eernst
2016/06/29 09:28:31
Done.
| |
181 | 225 |
182 Element add(Element newElement) { | 226 Element add(Element newElement) { |
183 throw "Cannot add an element to a library scope"; | 227 throw "Cannot add an element to a library scope"; |
184 } | 228 } |
185 | 229 |
186 String toString() => 'LibraryScope($library)'; | 230 String toString() => 'LibraryScope($library)'; |
187 } | 231 } |
OLD | NEW |