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.kernel_enum_builder; | 5 library fasta.kernel_enum_builder; |
6 | 6 |
7 import 'package:kernel/ast.dart' | 7 import 'package:kernel/ast.dart' |
8 show | 8 show |
9 Arguments, | 9 Arguments, |
10 Class, | 10 Class, |
(...skipping 12 matching lines...) Expand all Loading... |
23 ProcedureKind, | 23 ProcedureKind, |
24 ReturnStatement, | 24 ReturnStatement, |
25 StaticGet, | 25 StaticGet, |
26 StringLiteral, | 26 StringLiteral, |
27 SuperInitializer, | 27 SuperInitializer, |
28 ThisExpression, | 28 ThisExpression, |
29 VariableGet; | 29 VariableGet; |
30 | 30 |
31 import 'package:front_end/src/scanner/token.dart' show Token; | 31 import 'package:front_end/src/scanner/token.dart' show Token; |
32 | 32 |
33 import '../errors.dart' show inputError; | |
34 | |
35 import '../modifier.dart' show constMask, finalMask, staticMask; | 33 import '../modifier.dart' show constMask, finalMask, staticMask; |
36 | 34 |
37 import '../names.dart' show indexGetName; | 35 import '../names.dart' show indexGetName; |
38 | 36 |
39 import '../source/source_class_builder.dart' show SourceClassBuilder; | 37 import '../source/source_class_builder.dart' show SourceClassBuilder; |
40 | 38 |
41 import 'kernel_builder.dart' | 39 import 'kernel_builder.dart' |
42 show | 40 show |
43 Builder, | 41 Builder, |
44 EnumBuilder, | 42 EnumBuilder, |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 parent, | 166 parent, |
169 charOffset, | 167 charOffset, |
170 charOffset, | 168 charOffset, |
171 charEndOffset); | 169 charEndOffset); |
172 members["toString"] = toStringBuilder; | 170 members["toString"] = toStringBuilder; |
173 String className = name; | 171 String className = name; |
174 for (int i = 0; i < constantNamesAndOffsets.length; i += 2) { | 172 for (int i = 0; i < constantNamesAndOffsets.length; i += 2) { |
175 String name = constantNamesAndOffsets[i]; | 173 String name = constantNamesAndOffsets[i]; |
176 int charOffset = constantNamesAndOffsets[i + 1]; | 174 int charOffset = constantNamesAndOffsets[i + 1]; |
177 if (members.containsKey(name)) { | 175 if (members.containsKey(name)) { |
178 inputError(null, null, "Duplicated name: $name"); | 176 parent.addCompileTimeError(charOffset, "Duplicated name: '$name'."); |
| 177 constantNamesAndOffsets[i] = null; |
| 178 continue; |
| 179 } |
| 180 if (name == className) { |
| 181 parent.addCompileTimeError( |
| 182 charOffset, |
| 183 "Name of enum constant '$name' can't be the same as the enum's " |
| 184 "own name."); |
| 185 constantNamesAndOffsets[i] = null; |
179 continue; | 186 continue; |
180 } | 187 } |
181 KernelFieldBuilder fieldBuilder = new KernelFieldBuilder( | 188 KernelFieldBuilder fieldBuilder = new KernelFieldBuilder( |
182 null, | 189 null, |
183 selfType, | 190 selfType, |
184 name, | 191 name, |
185 constMask | staticMask, | 192 constMask | staticMask, |
186 parent, | 193 parent, |
187 charOffset, | 194 charOffset, |
188 // Synthetic token to signal this field is initialized. | 195 // Synthetic token to signal this field is initialized. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 KernelProcedureBuilder toStringBuilder = this["toString"]; | 250 KernelProcedureBuilder toStringBuilder = this["toString"]; |
244 toStringBuilder.body = new ReturnStatement(new MethodInvocation( | 251 toStringBuilder.body = new ReturnStatement(new MethodInvocation( |
245 toStringMap, | 252 toStringMap, |
246 indexGetName, | 253 indexGetName, |
247 new Arguments(<Expression>[ | 254 new Arguments(<Expression>[ |
248 new DirectPropertyGet(new ThisExpression(), indexField) | 255 new DirectPropertyGet(new ThisExpression(), indexField) |
249 ]))); | 256 ]))); |
250 List<Expression> values = <Expression>[]; | 257 List<Expression> values = <Expression>[]; |
251 for (int i = 0; i < constantNamesAndOffsets.length; i += 2) { | 258 for (int i = 0; i < constantNamesAndOffsets.length; i += 2) { |
252 String name = constantNamesAndOffsets[i]; | 259 String name = constantNamesAndOffsets[i]; |
253 KernelFieldBuilder builder = this[name]; | 260 if (name != null) { |
254 values.add(new StaticGet(builder.build(libraryBuilder))); | 261 KernelFieldBuilder builder = this[name]; |
| 262 values.add(new StaticGet(builder.build(libraryBuilder))); |
| 263 } |
255 } | 264 } |
256 KernelFieldBuilder valuesBuilder = this["values"]; | 265 KernelFieldBuilder valuesBuilder = this["values"]; |
257 valuesBuilder.build(libraryBuilder); | 266 valuesBuilder.build(libraryBuilder); |
258 valuesBuilder.initializer = | 267 valuesBuilder.initializer = |
259 new ListLiteral(values, typeArgument: cls.rawType, isConst: true); | 268 new ListLiteral(values, typeArgument: cls.rawType, isConst: true); |
260 KernelConstructorBuilder constructorBuilder = constructorScopeBuilder[""]; | 269 KernelConstructorBuilder constructorBuilder = constructorScopeBuilder[""]; |
261 Constructor constructor = constructorBuilder.build(libraryBuilder); | 270 Constructor constructor = constructorBuilder.build(libraryBuilder); |
262 constructor.initializers.insert( | 271 constructor.initializers.insert( |
263 0, | 272 0, |
264 new FieldInitializer(indexField, | 273 new FieldInitializer(indexField, |
265 new VariableGet(constructor.function.positionalParameters.single)) | 274 new VariableGet(constructor.function.positionalParameters.single)) |
266 ..parent = constructor); | 275 ..parent = constructor); |
267 KernelClassBuilder objectClass = objectType.builder; | 276 KernelClassBuilder objectClass = objectType.builder; |
268 MemberBuilder superConstructor = objectClass.findConstructorOrFactory( | 277 MemberBuilder superConstructor = objectClass.findConstructorOrFactory( |
269 "", charOffset, fileUri, libraryBuilder); | 278 "", charOffset, fileUri, libraryBuilder); |
270 if (superConstructor == null || !superConstructor.isConstructor) { | 279 if (superConstructor == null || !superConstructor.isConstructor) { |
271 // TODO(ahe): Ideally, we would also want to check that [Object]'s | 280 // TODO(ahe): Ideally, we would also want to check that [Object]'s |
272 // unnamed constructor requires no arguments. But that information isn't | 281 // unnamed constructor requires no arguments. But that information isn't |
273 // always available at this point, and it's not really a situation that | 282 // always available at this point, and it's not really a situation that |
274 // can happen unless you start modifying the SDK sources. | 283 // can happen unless you start modifying the SDK sources. |
275 addCompileTimeError(-1, "'Object' has no unnamed constructor."); | 284 addCompileTimeError(-1, "'Object' has no unnamed constructor."); |
276 } else { | 285 } else { |
277 constructor.initializers.add( | 286 constructor.initializers.add( |
278 new SuperInitializer(superConstructor.target, new Arguments.empty()) | 287 new SuperInitializer(superConstructor.target, new Arguments.empty()) |
279 ..parent = constructor); | 288 ..parent = constructor); |
280 } | 289 } |
281 int index = 0; | 290 int index = 0; |
282 for (int i = 0; i < constantNamesAndOffsets.length; i += 2) { | 291 for (int i = 0; i < constantNamesAndOffsets.length; i += 2) { |
283 String constant = constantNamesAndOffsets[i]; | 292 String constant = constantNamesAndOffsets[i]; |
284 KernelFieldBuilder field = this[constant]; | 293 if (constant != null) { |
285 field.build(libraryBuilder); | 294 KernelFieldBuilder field = this[constant]; |
286 Arguments arguments = | 295 field.build(libraryBuilder); |
287 new Arguments(<Expression>[new IntLiteral(index++)]); | 296 Arguments arguments = |
288 field.initializer = | 297 new Arguments(<Expression>[new IntLiteral(index++)]); |
289 new ConstructorInvocation(constructor, arguments, isConst: true); | 298 field.initializer = |
| 299 new ConstructorInvocation(constructor, arguments, isConst: true); |
| 300 } |
290 } | 301 } |
291 return super.build(libraryBuilder, coreLibrary); | 302 return super.build(libraryBuilder, coreLibrary); |
292 } | 303 } |
293 | 304 |
294 @override | 305 @override |
295 Builder findConstructorOrFactory( | 306 Builder findConstructorOrFactory( |
296 String name, int charOffset, Uri uri, LibraryBuilder library) { | 307 String name, int charOffset, Uri uri, LibraryBuilder library) { |
297 return null; | 308 return null; |
298 } | 309 } |
299 } | 310 } |
OLD | NEW |