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_field_builder; | 5 library fasta.kernel_field_builder; |
6 | 6 |
7 import 'package:front_end/src/fasta/kernel/body_builder.dart' show BodyBuilder; | 7 import 'package:front_end/src/fasta/kernel/body_builder.dart' show BodyBuilder; |
8 | 8 |
9 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' | 9 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' |
10 show KernelField; | 10 show KernelField; |
11 | 11 |
12 import 'package:front_end/src/fasta/parser/parser.dart' show Parser; | 12 import 'package:front_end/src/fasta/parser/parser.dart' show Parser; |
13 | 13 |
14 import 'package:front_end/src/scanner/token.dart' show Token; | 14 import 'package:front_end/src/scanner/token.dart' show Token; |
15 | 15 |
16 import 'package:front_end/src/fasta/builder/class_builder.dart' | 16 import 'package:front_end/src/fasta/builder/class_builder.dart' |
17 show ClassBuilder; | 17 show ClassBuilder; |
18 | 18 |
19 import 'package:front_end/src/fasta/source/source_library_builder.dart' | 19 import 'package:front_end/src/fasta/source/source_library_builder.dart' |
20 show SourceLibraryBuilder; | 20 show SourceLibraryBuilder; |
21 | 21 |
22 import 'package:front_end/src/fasta/type_inference/type_inference_listener.dart' | 22 import 'package:front_end/src/fasta/type_inference/type_inference_listener.dart' |
23 show TypeInferenceListener; | 23 show TypeInferenceListener; |
24 | 24 |
25 import 'package:kernel/ast.dart' show DartType, Expression, Field, Name; | 25 import 'package:kernel/ast.dart' |
| 26 show DartType, Expression, Field, Name, NullLiteral; |
| 27 |
| 28 import '../errors.dart' show internalError; |
26 | 29 |
27 import 'kernel_builder.dart' | 30 import 'kernel_builder.dart' |
28 show Builder, FieldBuilder, KernelTypeBuilder, MetadataBuilder; | 31 show Builder, FieldBuilder, KernelTypeBuilder, MetadataBuilder; |
29 | 32 |
30 class KernelFieldBuilder extends FieldBuilder<Expression> { | 33 class KernelFieldBuilder extends FieldBuilder<Expression> { |
31 final Field field; | 34 final Field field; |
32 final List<MetadataBuilder> metadata; | 35 final List<MetadataBuilder> metadata; |
33 final KernelTypeBuilder type; | 36 final KernelTypeBuilder type; |
34 final Token initializerToken; | 37 final Token initializerToken; |
35 | 38 |
36 KernelFieldBuilder(this.metadata, this.type, String name, int modifiers, | 39 KernelFieldBuilder(this.metadata, this.type, String name, int modifiers, |
37 Builder compilationUnit, int charOffset, this.initializerToken) | 40 Builder compilationUnit, int charOffset, this.initializerToken) |
38 : field = new KernelField(null, fileUri: compilationUnit?.relativeFileUri) | 41 : field = new KernelField(null, fileUri: compilationUnit?.relativeFileUri) |
39 ..fileOffset = charOffset, | 42 ..fileOffset = charOffset, |
40 super(name, modifiers, compilationUnit, charOffset); | 43 super(name, modifiers, compilationUnit, charOffset); |
41 | 44 |
| 45 bool get hasInitializer => initializerToken != null; |
| 46 |
42 void set initializer(Expression value) { | 47 void set initializer(Expression value) { |
| 48 if (!hasInitializer && value is! NullLiteral && !isConst && !isFinal) { |
| 49 internalError("Attempt to set initializer on field without initializer."); |
| 50 } |
43 field.initializer = value..parent = field; | 51 field.initializer = value..parent = field; |
44 } | 52 } |
45 | 53 |
46 Field build(SourceLibraryBuilder library) { | 54 Field build(SourceLibraryBuilder library) { |
47 field.name ??= new Name(name, library.target); | 55 field.name ??= new Name(name, library.target); |
48 if (type != null) { | 56 if (type != null) { |
49 field.type = type.build(library); | 57 field.type = type.build(library); |
50 } | 58 } |
51 bool isInstanceMember = !isStatic && !isTopLevel; | 59 bool isInstanceMember = !isStatic && !isTopLevel; |
52 field | 60 field |
53 ..isFinal = isFinal | 61 ..isFinal = isFinal |
54 ..isConst = isConst | 62 ..isConst = isConst |
55 ..hasImplicitGetter = isInstanceMember | 63 ..hasImplicitGetter = isInstanceMember |
56 ..hasImplicitSetter = isInstanceMember && !isConst && !isFinal | 64 ..hasImplicitSetter = isInstanceMember && !isConst && !isFinal |
57 ..isStatic = !isInstanceMember; | 65 ..isStatic = !isInstanceMember; |
58 if (initializerToken != null) { | 66 if (initializerToken != null && !initializerToken.isEof) { |
59 library.loader.typeInferenceEngine.recordField(field); | 67 library.loader.typeInferenceEngine.recordField(field); |
60 } | 68 } |
61 return field; | 69 return field; |
62 } | 70 } |
63 | 71 |
64 Field get target => field; | 72 Field get target => field; |
65 | 73 |
66 @override | 74 @override |
67 void prepareInitializerInference( | 75 void prepareInitializerInference( |
68 SourceLibraryBuilder library, ClassBuilder currentClass) { | 76 SourceLibraryBuilder library, ClassBuilder currentClass) { |
69 if (initializerToken != null) { | 77 if (initializerToken != null && !initializerToken.isEof) { |
70 var memberScope = | 78 var memberScope = |
71 currentClass == null ? library.scope : currentClass.scope; | 79 currentClass == null ? library.scope : currentClass.scope; |
72 // TODO(paulberry): Is it correct to pass library.uri into BodyBuilder, or | 80 // TODO(paulberry): Is it correct to pass library.uri into BodyBuilder, or |
73 // should it be the part URI? | 81 // should it be the part URI? |
74 var typeInferenceEngine = library.loader.typeInferenceEngine; | 82 var typeInferenceEngine = library.loader.typeInferenceEngine; |
75 var listener = new TypeInferenceListener(); | 83 var listener = new TypeInferenceListener(); |
76 var typeInferrer = | 84 var typeInferrer = |
77 typeInferenceEngine.createTopLevelTypeInferrer(field, listener); | 85 typeInferenceEngine.createTopLevelTypeInferrer(field, listener); |
78 var bodyBuilder = new BodyBuilder( | 86 var bodyBuilder = new BodyBuilder( |
79 library, | 87 library, |
(...skipping 10 matching lines...) Expand all Loading... |
90 Token token = parser.parseExpression(initializerToken); | 98 Token token = parser.parseExpression(initializerToken); |
91 Expression expression = bodyBuilder.popForValue(); | 99 Expression expression = bodyBuilder.popForValue(); |
92 bodyBuilder.checkEmpty(token.charOffset); | 100 bodyBuilder.checkEmpty(token.charOffset); |
93 initializer = expression; | 101 initializer = expression; |
94 } | 102 } |
95 } | 103 } |
96 | 104 |
97 @override | 105 @override |
98 DartType get builtType => field.type; | 106 DartType get builtType => field.type; |
99 } | 107 } |
OLD | NEW |