OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 part of dart2js.js_emitter.program_builder; | 5 part of dart2js.js_emitter.program_builder; |
6 | 6 |
7 /** | 7 /** |
8 * [member] is a field (instance, static, or top level). | 8 * [member] is a field (instance, static, or top level). |
9 * | 9 * |
10 * [name] is the field name that the [Namer] has picked for this field's | 10 * [name] is the field name that the [Namer] has picked for this field's |
(...skipping 10 matching lines...) Expand all Loading... |
21 * [needsCheckedSetter] indicates that a checked getter is needed, and in this | 21 * [needsCheckedSetter] indicates that a checked getter is needed, and in this |
22 * case, [needsSetter] is always false. [needsCheckedSetter] is only true when | 22 * case, [needsSetter] is always false. [needsCheckedSetter] is only true when |
23 * type assertions are enabled (checked mode). | 23 * type assertions are enabled (checked mode). |
24 */ | 24 */ |
25 typedef void AcceptField(FieldEntity member, js.Name name, js.Name accessorName, | 25 typedef void AcceptField(FieldEntity member, js.Name name, js.Name accessorName, |
26 bool needsGetter, bool needsSetter, bool needsCheckedSetter); | 26 bool needsGetter, bool needsSetter, bool needsCheckedSetter); |
27 | 27 |
28 class FieldVisitor { | 28 class FieldVisitor { |
29 final CompilerOptions _options; | 29 final CompilerOptions _options; |
30 final ElementEnvironment _elementEnvironment; | 30 final ElementEnvironment _elementEnvironment; |
| 31 final CommonElements _commonElements; |
31 final CodegenWorldBuilder _codegenWorldBuilder; | 32 final CodegenWorldBuilder _codegenWorldBuilder; |
32 final NativeData _nativeData; | 33 final NativeData _nativeData; |
33 final MirrorsData _mirrorsData; | 34 final MirrorsData _mirrorsData; |
34 final Namer _namer; | 35 final Namer _namer; |
35 final ClosedWorld _closedWorld; | 36 final ClosedWorld _closedWorld; |
36 | 37 |
37 FieldVisitor( | 38 FieldVisitor( |
38 this._options, | 39 this._options, |
39 this._elementEnvironment, | 40 this._elementEnvironment, |
| 41 this._commonElements, |
40 this._codegenWorldBuilder, | 42 this._codegenWorldBuilder, |
41 this._nativeData, | 43 this._nativeData, |
42 this._mirrorsData, | 44 this._mirrorsData, |
43 this._namer, | 45 this._namer, |
44 this._closedWorld); | 46 this._closedWorld); |
45 | 47 |
46 /** | 48 /** |
47 * Invokes [f] for each of the fields of [element]. | 49 * Invokes [f] for each of the fields of [element]. |
48 * | 50 * |
49 * [element] must be a [ClassElement] or a [LibraryElement]. | 51 * [element] must be a [ClassEntity] or a [LibraryEntity]. |
50 * | 52 * |
51 * If [element] is a [ClassElement], the static fields of the class are | 53 * If [element] is a [ClassEntity], the static fields of the class are |
52 * visited if [visitStatics] is true and the instance fields are visited if | 54 * visited if [visitStatics] is true and the instance fields are visited if |
53 * [visitStatics] is false. | 55 * [visitStatics] is false. |
54 * | 56 * |
55 * If [element] is a [LibraryElement], [visitStatics] must be true. | 57 * If [element] is a [LibraryEntity], [visitStatics] must be true. |
56 * | 58 * |
57 * When visiting the instance fields of a class, the fields of its superclass | 59 * When visiting the instance fields of a class, the fields of its superclass |
58 * are also visited if the class is instantiated. | 60 * are also visited if the class is instantiated. |
59 * | 61 * |
60 * Invariant: [element] must be a declaration element. | 62 * Invariant: [element] must be a declaration element. |
61 */ | 63 */ |
62 void visitFields(AcceptField f, | 64 void visitFields(AcceptField f, |
63 {bool visitStatics: false, LibraryEntity library, ClassEntity cls}) { | 65 {bool visitStatics: false, LibraryEntity library, ClassEntity cls}) { |
64 assert(!(library is LibraryElement && !library.isDeclaration), | 66 assert(!(library is LibraryElement && !library.isDeclaration), |
65 failedAt(library)); | 67 failedAt(library)); |
66 assert(!(cls is ClassElement && !cls.isDeclaration), failedAt(cls)); | 68 assert(!(cls is ClassElement && !cls.isDeclaration), failedAt(cls)); |
67 | 69 |
68 bool isNativeClass = false; | 70 bool isNativeClass = false; |
69 bool isLibrary = false; | 71 bool isLibrary = false; |
70 bool isInstantiated = false; | 72 bool isInstantiated = false; |
71 if (cls != null) { | 73 if (cls != null) { |
72 isNativeClass = _nativeData.isNativeClass(cls); | 74 isNativeClass = _nativeData.isNativeClass(cls); |
73 | 75 |
74 // If the class is never instantiated we still need to set it up for | 76 // If the class is never instantiated we still need to set it up for |
75 // inheritance purposes, but we can simplify its JavaScript constructor. | 77 // inheritance purposes, but we can simplify its JavaScript constructor. |
76 isInstantiated = | 78 isInstantiated = |
77 _codegenWorldBuilder.directlyInstantiatedClasses.contains(cls); | 79 _codegenWorldBuilder.directlyInstantiatedClasses.contains(cls); |
78 } else if (library != null) { | 80 } else if (library != null) { |
79 isLibrary = true; | 81 isLibrary = true; |
80 assert(visitStatics, failedAt(library)); | 82 assert(visitStatics, failedAt(library)); |
81 } else { | 83 } else { |
82 failedAt(NO_LOCATION_SPANNABLE, | 84 failedAt( |
83 'Expected a ClassElement or a LibraryElement.'); | 85 NO_LOCATION_SPANNABLE, 'Expected a ClassEntity or a LibraryEntity.'); |
84 } | 86 } |
85 | 87 |
86 void visitField(FieldEntity field, {ClassEntity holder}) { | 88 void visitField(FieldEntity field, {ClassEntity holder}) { |
87 assert(!(field is FieldElement && !field.isDeclaration), failedAt(field)); | 89 assert(!(field is FieldElement && !field.isDeclaration), failedAt(field)); |
88 | 90 |
89 bool isMixinNativeField = | 91 bool isMixinNativeField = |
90 isNativeClass && _elementEnvironment.isMixinApplication(holder); | 92 isNativeClass && _elementEnvironment.isMixinApplication(holder); |
91 | 93 |
92 // See if we can dynamically create getters and setters. | 94 // See if we can dynamically create getters and setters. |
93 // We can only generate getters and setters for [element] since | 95 // We can only generate getters and setters for [element] since |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 170 |
169 static bool fieldAccessNeverThrows(FieldEntity field) { | 171 static bool fieldAccessNeverThrows(FieldEntity field) { |
170 return | 172 return |
171 // We never access a field in a closure (a captured variable) without | 173 // We never access a field in a closure (a captured variable) without |
172 // knowing that it is there. Therefore we don't need to use a getter | 174 // knowing that it is there. Therefore we don't need to use a getter |
173 // (that will throw if the getter method is missing), but can always | 175 // (that will throw if the getter method is missing), but can always |
174 // access the field directly. | 176 // access the field directly. |
175 field is ClosureFieldElement; | 177 field is ClosureFieldElement; |
176 } | 178 } |
177 | 179 |
178 bool canAvoidGeneratedCheckedSetter(FieldElement member) { | 180 bool canAvoidGeneratedCheckedSetter(FieldEntity member) { |
179 // We never generate accessors for top-level/static fields. | 181 // We never generate accessors for top-level/static fields. |
180 if (!member.isInstanceMember) return true; | 182 if (!member.isInstanceMember) return true; |
181 ResolutionDartType type = member.type; | 183 DartType type = _elementEnvironment.getFieldType(member); |
182 return type.treatAsDynamic || type.isObject; | 184 return type.treatAsDynamic || type == _commonElements.objectType; |
183 } | 185 } |
184 } | 186 } |
OLD | NEW |