OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.scope; | 5 library fasta.scope; |
6 | 6 |
7 import 'builder/builder.dart' show Builder, MixedAccessor; | 7 import 'builder/builder.dart' show Builder, MixedAccessor; |
8 | 8 |
9 import 'errors.dart' show internalError; | 9 import 'errors.dart' show internalError; |
10 | 10 |
11 class Scope { | 11 class Scope { |
12 /// Names declared in this scope. | 12 /// Names declared in this scope. |
13 final Map<String, Builder> local; | 13 final Map<String, Builder> local; |
14 | 14 |
| 15 /// Setters declared in this scope. |
| 16 final Map<String, Builder> setters; |
| 17 |
15 /// The scope that this scope is nested within, or `null` if this is the top | 18 /// The scope that this scope is nested within, or `null` if this is the top |
16 /// level scope. | 19 /// level scope. |
17 final Scope parent; | 20 final Scope parent; |
18 | 21 |
19 /// Indicates whether an attempt to declare new names in this scope should | 22 /// Indicates whether an attempt to declare new names in this scope should |
20 /// succeed. | 23 /// succeed. |
21 final bool isModifiable; | 24 final bool isModifiable; |
22 | 25 |
23 Map<String, Builder> labels; | 26 Map<String, Builder> labels; |
24 | 27 |
25 Map<String, Builder> forwardDeclaredLabels; | 28 Map<String, Builder> forwardDeclaredLabels; |
26 | 29 |
27 Scope(this.local, this.parent, {this.isModifiable: true}); | 30 Scope(this.local, Map<String, Builder> setters, this.parent, |
| 31 {this.isModifiable: true}) |
| 32 : setters = setters ?? const <String, Builder>{}; |
| 33 |
| 34 Scope.top({bool isModifiable: false}) |
| 35 : this(<String, Builder>{}, <String, Builder>{}, null, |
| 36 isModifiable: isModifiable); |
| 37 |
| 38 Scope.immutable() |
| 39 : this(const <String, Builder>{}, const <String, Builder>{}, null, |
| 40 isModifiable: false); |
| 41 |
| 42 Scope.nested(Scope parent, {bool isModifiable: true}) |
| 43 : this(<String, Builder>{}, null, parent, isModifiable: isModifiable); |
28 | 44 |
29 Scope createNestedScope({bool isModifiable: true}) { | 45 Scope createNestedScope({bool isModifiable: true}) { |
30 return new Scope(<String, Builder>{}, this, isModifiable: isModifiable); | 46 return new Scope.nested(this, isModifiable: isModifiable); |
31 } | 47 } |
32 | 48 |
33 Builder lookup(String name, int charOffset, Uri fileUri) { | 49 Builder lookup(String name, int charOffset, Uri fileUri, |
| 50 {bool isInstanceScope: true}) { |
34 Builder builder = local[name]; | 51 Builder builder = local[name]; |
35 if (builder != null) { | 52 if (builder != null) { |
36 if (builder.next != null) { | 53 if (builder.next != null) { |
37 return lookupAmbiguous(name, builder, false, charOffset, fileUri); | 54 return lookupAmbiguous(name, builder, false, charOffset, fileUri); |
38 } | 55 } |
39 return builder.isSetter | 56 return builder.isSetter |
40 ? new AccessErrorBuilder(name, builder, charOffset, fileUri) | 57 ? new AccessErrorBuilder(name, builder, charOffset, fileUri) |
41 : builder; | 58 : builder; |
42 } else { | 59 } else { |
43 return parent?.lookup(name, charOffset, fileUri); | 60 return parent?.lookup(name, charOffset, fileUri); |
44 } | 61 } |
45 } | 62 } |
46 | 63 |
47 Builder lookupSetter(String name, int charOffset, Uri fileUri) { | 64 Builder lookupSetter(String name, int charOffset, Uri fileUri, |
| 65 {bool isInstanceScope: true}) { |
48 Builder builder = local[name]; | 66 Builder builder = local[name]; |
49 if (builder != null) { | 67 if (builder != null) { |
50 if (builder.next != null) { | 68 if (builder.next != null) { |
51 return lookupAmbiguous(name, builder, true, charOffset, fileUri); | 69 return lookupAmbiguous(name, builder, true, charOffset, fileUri); |
52 } | 70 } |
53 if (builder.isField) { | 71 if (builder.isField) { |
54 if (builder.isFinal) { | 72 if (builder.isFinal) { |
55 return new AccessErrorBuilder(name, builder, charOffset, fileUri); | 73 return new AccessErrorBuilder(name, builder, charOffset, fileUri); |
56 } else { | 74 } else { |
57 return builder; | 75 return builder; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 142 } |
125 | 143 |
126 // TODO(ahe): Rename to extend or something. | 144 // TODO(ahe): Rename to extend or something. |
127 void operator []=(String name, Builder member) { | 145 void operator []=(String name, Builder member) { |
128 if (isModifiable) { | 146 if (isModifiable) { |
129 local[name] = member; | 147 local[name] = member; |
130 } else { | 148 } else { |
131 internalError("Can't extend an unmodifiable scope."); | 149 internalError("Can't extend an unmodifiable scope."); |
132 } | 150 } |
133 } | 151 } |
| 152 |
| 153 void forEach(f(String name, Builder member)) { |
| 154 local.forEach(f); |
| 155 } |
134 } | 156 } |
135 | 157 |
136 abstract class ProblemBuilder extends Builder { | 158 abstract class ProblemBuilder extends Builder { |
137 final String name; | 159 final String name; |
138 | 160 |
139 final Builder builder; | 161 final Builder builder; |
140 | 162 |
141 ProblemBuilder(this.name, this.builder, int charOffset, Uri fileUri) | 163 ProblemBuilder(this.name, this.builder, int charOffset, Uri fileUri) |
142 : super(null, charOffset, fileUri); | 164 : super(null, charOffset, fileUri); |
143 | 165 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 | 203 |
182 String get message => "Access error: '$name'."; | 204 String get message => "Access error: '$name'."; |
183 } | 205 } |
184 | 206 |
185 class AmbiguousBuilder extends ProblemBuilder { | 207 class AmbiguousBuilder extends ProblemBuilder { |
186 AmbiguousBuilder(String name, Builder builder, int charOffset, Uri fileUri) | 208 AmbiguousBuilder(String name, Builder builder, int charOffset, Uri fileUri) |
187 : super(name, builder, charOffset, fileUri); | 209 : super(name, builder, charOffset, fileUri); |
188 | 210 |
189 String get message => "Duplicated named: '$name'."; | 211 String get message => "Duplicated named: '$name'."; |
190 } | 212 } |
OLD | NEW |