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 * Adds [element] to this scope. This operation is only allowed on mutable |
13 * scopes such as [MethodScope] and [BlockScope]. | 13 * scopes such as [MethodScope] and [BlockScope]. |
14 */ | 14 */ |
15 Element add(Element element); | 15 Element add(Element element); |
16 | 16 |
17 /** | 17 /** |
18 * Looks up the [Element] for [name] in this scope. | 18 * Looks up the [Element] for [name] in this scope. |
19 */ | 19 */ |
20 Element lookup(String name); | 20 Element lookup(String name); |
21 | 21 |
22 static Scope buildEnclosingScope(Element element) { | 22 static Scope buildEnclosingScope(Element element) { |
23 return element.enclosingElement != null | 23 return element.enclosingElement != null |
24 ? element.enclosingElement.buildScope() : element.buildScope(); | 24 ? element.enclosingElement.buildScope() |
| 25 : element.buildScope(); |
25 } | 26 } |
26 } | 27 } |
27 | 28 |
28 abstract class NestedScope extends Scope { | 29 abstract class NestedScope extends Scope { |
29 final Scope parent; | 30 final Scope parent; |
30 | 31 |
31 NestedScope(this.parent); | 32 NestedScope(this.parent); |
32 | 33 |
33 Element lookup(String name) { | 34 Element lookup(String name) { |
34 Element result = localLookup(name); | 35 Element result = localLookup(name); |
(...skipping 25 matching lines...) Expand all Loading... |
60 /** | 61 /** |
61 * [TypeDeclarationScope] defines the outer scope of a type declaration in | 62 * [TypeDeclarationScope] defines the outer scope of a type declaration in |
62 * which the declared type variables and the entities in the enclosing scope are | 63 * which the declared type variables and the entities in the enclosing scope are |
63 * available but where declared and inherited members are not available. This | 64 * available but where declared and inherited members are not available. This |
64 * scope is only used for class declarations during resolution of the | 65 * scope is only used for class declarations during resolution of the |
65 * class hierarchy. In all other cases [ClassScope] is used. | 66 * class hierarchy. In all other cases [ClassScope] is used. |
66 */ | 67 */ |
67 class TypeDeclarationScope extends NestedScope { | 68 class TypeDeclarationScope extends NestedScope { |
68 final TypeDeclarationElement element; | 69 final TypeDeclarationElement element; |
69 | 70 |
70 TypeDeclarationScope(parent, this.element) | 71 TypeDeclarationScope(parent, this.element) : super(parent) { |
71 : super(parent) { | |
72 assert(parent != null); | 72 assert(parent != null); |
73 } | 73 } |
74 | 74 |
75 Element add(Element newElement) { | 75 Element add(Element newElement) { |
76 throw "Cannot add element to TypeDeclarationScope"; | 76 throw "Cannot add element to TypeDeclarationScope"; |
77 } | 77 } |
78 | 78 |
79 Element lookupTypeVariable(String name) { | 79 Element lookupTypeVariable(String name) { |
80 List<DartType> typeVariables = element.typeVariables; | 80 List<DartType> typeVariables = element.typeVariables; |
81 for (TypeVariableType type in typeVariables) { | 81 for (TypeVariableType type in typeVariables) { |
82 if (type.name == name) { | 82 if (type.name == name) { |
83 return type.element; | 83 return type.element; |
84 } | 84 } |
85 } | 85 } |
86 return null; | 86 return null; |
87 } | 87 } |
88 | 88 |
89 Element localLookup(String name) => lookupTypeVariable(name); | 89 Element localLookup(String name) => lookupTypeVariable(name); |
90 | 90 |
91 String toString() => | 91 String toString() => 'TypeDeclarationScope($element)'; |
92 'TypeDeclarationScope($element)'; | |
93 } | 92 } |
94 | 93 |
95 abstract class MutableScope extends NestedScope { | 94 abstract class MutableScope extends NestedScope { |
96 final Map<String, Element> elements; | 95 final Map<String, Element> elements; |
97 | 96 |
98 MutableScope(Scope parent) | 97 MutableScope(Scope parent) |
99 : super(parent), | 98 : super(parent), |
100 this.elements = new Map<String, Element>() { | 99 this.elements = new Map<String, Element>() { |
101 assert(parent != null); | 100 assert(parent != null); |
102 } | 101 } |
103 | 102 |
104 Element add(Element newElement) { | 103 Element add(Element newElement) { |
105 if (elements.containsKey(newElement.name)) { | 104 if (elements.containsKey(newElement.name)) { |
106 return elements[newElement.name]; | 105 return elements[newElement.name]; |
107 } | 106 } |
108 elements[newElement.name] = newElement; | 107 elements[newElement.name] = newElement; |
109 return newElement; | 108 return newElement; |
110 } | 109 } |
111 | 110 |
112 Element localLookup(String name) => elements[name]; | 111 Element localLookup(String name) => elements[name]; |
113 } | 112 } |
114 | 113 |
115 class MethodScope extends MutableScope { | 114 class MethodScope extends MutableScope { |
116 final Element element; | 115 final Element element; |
117 | 116 |
118 MethodScope(Scope parent, this.element) | 117 MethodScope(Scope parent, this.element) : super(parent); |
119 : super(parent); | |
120 | 118 |
121 String toString() => 'MethodScope($element${elements.keys.toList()})'; | 119 String toString() => 'MethodScope($element${elements.keys.toList()})'; |
122 } | 120 } |
123 | 121 |
124 class BlockScope extends MutableScope { | 122 class BlockScope extends MutableScope { |
125 BlockScope(Scope parent) : super(parent); | 123 BlockScope(Scope parent) : super(parent); |
126 | 124 |
127 String toString() => 'BlockScope(${elements.keys.toList()})'; | 125 String toString() => 'BlockScope(${elements.keys.toList()})'; |
128 } | 126 } |
129 | 127 |
130 /** | 128 /** |
131 * [ClassScope] defines the inner scope of a class/interface declaration in | 129 * [ClassScope] defines the inner scope of a class/interface declaration in |
132 * which declared members, declared type variables, entities in the enclosing | 130 * which declared members, declared type variables, entities in the enclosing |
133 * scope and inherited members are available, in the given order. | 131 * scope and inherited members are available, in the given order. |
134 */ | 132 */ |
135 class ClassScope extends TypeDeclarationScope { | 133 class ClassScope extends TypeDeclarationScope { |
136 ClassElement get element => super.element; | 134 ClassElement get element => super.element; |
137 | 135 |
138 ClassScope(Scope parentScope, ClassElement element) | 136 ClassScope(Scope parentScope, ClassElement element) |
139 : super(parentScope, element) { | 137 : super(parentScope, element) { |
140 assert(parent != null); | 138 assert(parent != null); |
141 } | 139 } |
142 | 140 |
143 Element localLookup(String name) { | 141 Element localLookup(String name) { |
144 Element result = element.lookupLocalMember(name); | 142 Element result = element.lookupLocalMember(name); |
145 if (result != null) return result; | 143 if (result != null) return result; |
146 return super.localLookup(name); | 144 return super.localLookup(name); |
147 } | 145 } |
148 | 146 |
149 Element lookup(String name) { | 147 Element lookup(String name) { |
(...skipping 18 matching lines...) Expand all Loading... |
168 | 166 |
169 Element localLookup(String name) => library.find(name); | 167 Element localLookup(String name) => library.find(name); |
170 Element lookup(String name) => localLookup(name); | 168 Element lookup(String name) => localLookup(name); |
171 | 169 |
172 Element add(Element newElement) { | 170 Element add(Element newElement) { |
173 throw "Cannot add an element to a library scope"; | 171 throw "Cannot add an element to a library scope"; |
174 } | 172 } |
175 | 173 |
176 String toString() => 'LibraryScope($library)'; | 174 String toString() => 'LibraryScope($library)'; |
177 } | 175 } |
OLD | NEW |