| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class ClassEmitter extends CodeEmitterHelper { | 7 class ClassEmitter extends CodeEmitterHelper { |
| 8 /** | 8 /** |
| 9 * Documentation wanted -- johnniwinther | 9 * Documentation wanted -- johnniwinther |
| 10 * | 10 * |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 bool isLibrary = false; | 101 bool isLibrary = false; |
| 102 if (element.isClass()) { | 102 if (element.isClass()) { |
| 103 isClass = true; | 103 isClass = true; |
| 104 } else if (element.isLibrary()) { | 104 } else if (element.isLibrary()) { |
| 105 isLibrary = false; | 105 isLibrary = false; |
| 106 assert(invariant(element, emitStatics)); | 106 assert(invariant(element, emitStatics)); |
| 107 } else { | 107 } else { |
| 108 throw new SpannableAssertionFailure( | 108 throw new SpannableAssertionFailure( |
| 109 element, 'Must be a ClassElement or a LibraryElement'); | 109 element, 'Must be a ClassElement or a LibraryElement'); |
| 110 } | 110 } |
| 111 StringBuffer buffer = new StringBuffer(); |
| 111 if (emitStatics) { | 112 if (emitStatics) { |
| 112 assert(invariant(element, superName == null, message: superName)); | 113 assert(invariant(element, superName == null, message: superName)); |
| 113 } else { | 114 } else { |
| 114 assert(invariant(element, superName != null)); | 115 assert(invariant(element, superName != null)); |
| 115 String nativeName = | 116 String nativeName = |
| 116 namer.getPrimitiveInterceptorRuntimeName(element); | 117 namer.getPrimitiveInterceptorRuntimeName(element); |
| 117 if (nativeName != null) { | 118 if (nativeName != null) { |
| 118 builder.nativeName = nativeName; | 119 buffer.write('$nativeName/'); |
| 119 } | 120 } |
| 120 builder.superName = superName; | 121 buffer.write('$superName;'); |
| 121 } | 122 } |
| 123 int bufferClassLength = buffer.length; |
| 124 |
| 125 String separator = ''; |
| 126 |
| 122 var fieldMetadata = []; | 127 var fieldMetadata = []; |
| 123 bool hasMetadata = false; | 128 bool hasMetadata = false; |
| 124 bool fieldsAdded = false; | |
| 125 | 129 |
| 126 if (!onlyForRti) { | 130 if (!onlyForRti) { |
| 127 visitFields(element, emitStatics, | 131 visitFields(element, emitStatics, |
| 128 (VariableElement field, | 132 (VariableElement field, |
| 129 String name, | 133 String name, |
| 130 String accessorName, | 134 String accessorName, |
| 131 bool needsGetter, | 135 bool needsGetter, |
| 132 bool needsSetter, | 136 bool needsSetter, |
| 133 bool needsCheckedSetter) { | 137 bool needsCheckedSetter) { |
| 134 // Ignore needsCheckedSetter - that is handled below. | 138 // Ignore needsCheckedSetter - that is handled below. |
| 135 bool needsAccessor = (needsGetter || needsSetter); | 139 bool needsAccessor = (needsGetter || needsSetter); |
| 136 // We need to output the fields for non-native classes so we can auto- | 140 // We need to output the fields for non-native classes so we can auto- |
| 137 // generate the constructor. For native classes there are no | 141 // generate the constructor. For native classes there are no |
| 138 // constructors, so we don't need the fields unless we are generating | 142 // constructors, so we don't need the fields unless we are generating |
| 139 // accessors at runtime. | 143 // accessors at runtime. |
| 140 if (!classIsNative || needsAccessor) { | 144 if (!classIsNative || needsAccessor) { |
| 145 buffer.write(separator); |
| 146 separator = ','; |
| 141 var metadata = task.metadataEmitter.buildMetadataFunction(field); | 147 var metadata = task.metadataEmitter.buildMetadataFunction(field); |
| 142 if (metadata != null) { | 148 if (metadata != null) { |
| 143 hasMetadata = true; | 149 hasMetadata = true; |
| 144 } else { | 150 } else { |
| 145 metadata = new jsAst.LiteralNull(); | 151 metadata = new jsAst.LiteralNull(); |
| 146 } | 152 } |
| 147 fieldMetadata.add(metadata); | 153 fieldMetadata.add(metadata); |
| 148 recordMangledField(field, accessorName, field.name); | 154 recordMangledField(field, accessorName, field.name); |
| 149 String fieldName = name; | |
| 150 String fieldCode = ''; | |
| 151 String reflectionMarker = ''; | |
| 152 if (!needsAccessor) { | 155 if (!needsAccessor) { |
| 153 // Emit field for constructor generation. | 156 // Emit field for constructor generation. |
| 154 assert(!classIsNative); | 157 assert(!classIsNative); |
| 158 buffer.write(name); |
| 155 } else { | 159 } else { |
| 156 // Emit (possibly renaming) field name so we can add accessors at | 160 // Emit (possibly renaming) field name so we can add accessors at |
| 157 // runtime. | 161 // runtime. |
| 162 buffer.write(accessorName); |
| 158 if (name != accessorName) { | 163 if (name != accessorName) { |
| 159 fieldName = '$accessorName:$name'; | 164 buffer.write(':$name'); |
| 160 } | 165 } |
| 161 | 166 |
| 162 int getterCode = 0; | 167 int getterCode = 0; |
| 163 if (needsGetter) { | 168 if (needsGetter) { |
| 164 if (field.isInstanceMember()) { | 169 if (field.isInstanceMember()) { |
| 165 // 01: function() { return this.field; } | 170 // 01: function() { return this.field; } |
| 166 // 10: function(receiver) { return receiver.field; } | 171 // 10: function(receiver) { return receiver.field; } |
| 167 // 11: function(receiver) { return this.field; } | 172 // 11: function(receiver) { return this.field; } |
| 168 bool isIntercepted = backend.fieldHasInterceptedGetter(field); | 173 bool isIntercepted = backend.fieldHasInterceptedGetter(field); |
| 169 getterCode += isIntercepted ? 2 : 0; | 174 getterCode += isIntercepted ? 2 : 0; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 196 } | 201 } |
| 197 } else { | 202 } else { |
| 198 setterCode = 1; | 203 setterCode = 1; |
| 199 } | 204 } |
| 200 } | 205 } |
| 201 int code = getterCode + (setterCode << 2); | 206 int code = getterCode + (setterCode << 2); |
| 202 if (code == 0) { | 207 if (code == 0) { |
| 203 compiler.reportInternalError( | 208 compiler.reportInternalError( |
| 204 field, 'Internal error: code is 0 ($element/$field)'); | 209 field, 'Internal error: code is 0 ($element/$field)'); |
| 205 } else { | 210 } else { |
| 206 fieldCode = FIELD_CODE_CHARACTERS[code - FIRST_FIELD_CODE]; | 211 buffer.write(FIELD_CODE_CHARACTERS[code - FIRST_FIELD_CODE]); |
| 207 } | 212 } |
| 208 } | 213 } |
| 209 if (backend.isAccessibleByReflection(field)) { | 214 if (backend.isAccessibleByReflection(field)) { |
| 210 reflectionMarker = '-'; | 215 buffer.write('-'); |
| 211 if (backend.isNeededForReflection(field)) { | 216 if (backend.isNeededForReflection(field)) { |
| 212 DartType type = field.computeType(compiler); | 217 DartType type = field.computeType(compiler); |
| 213 reflectionMarker = '-${task.metadataEmitter.reifyType(type)}'; | 218 buffer.write('${task.metadataEmitter.reifyType(type)}'); |
| 214 } | 219 } |
| 215 } | 220 } |
| 216 builder.addField('$fieldName$fieldCode$reflectionMarker'); | |
| 217 fieldsAdded = true; | |
| 218 } | 221 } |
| 219 }); | 222 }); |
| 220 } | 223 } |
| 221 | 224 |
| 225 bool fieldsAdded = buffer.length > bufferClassLength; |
| 226 String compactClassData = buffer.toString(); |
| 227 jsAst.Expression classDataNode = js.string(compactClassData); |
| 222 if (hasMetadata) { | 228 if (hasMetadata) { |
| 223 builder.fieldMetadata = fieldMetadata; | 229 fieldMetadata.insert(0, classDataNode); |
| 230 classDataNode = new jsAst.ArrayInitializer.from(fieldMetadata); |
| 224 } | 231 } |
| 232 builder.addProperty('', classDataNode); |
| 225 return fieldsAdded; | 233 return fieldsAdded; |
| 226 } | 234 } |
| 227 | 235 |
| 228 void emitClassGettersSetters(ClassElement classElement, | 236 void emitClassGettersSetters(ClassElement classElement, |
| 229 ClassBuilder builder, | 237 ClassBuilder builder, |
| 230 {bool onlyForRti: false}) { | 238 {bool onlyForRti: false}) { |
| 231 if (onlyForRti) return; | 239 if (onlyForRti) return; |
| 232 | 240 |
| 233 visitFields(classElement, false, | 241 visitFields(classElement, false, |
| 234 (VariableElement member, | 242 (VariableElement member, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 if ((!typeVariableProperties.isEmpty && !hasSuper) || | 314 if ((!typeVariableProperties.isEmpty && !hasSuper) || |
| 307 (hasSuper && superclass.typeVariables != typeVars)) { | 315 (hasSuper && superclass.typeVariables != typeVars)) { |
| 308 classBuilder.addProperty('<>', | 316 classBuilder.addProperty('<>', |
| 309 new jsAst.ArrayInitializer.from(typeVariableProperties)); | 317 new jsAst.ArrayInitializer.from(typeVariableProperties)); |
| 310 } | 318 } |
| 311 } | 319 } |
| 312 | 320 |
| 313 List<jsAst.Property> statics = new List<jsAst.Property>(); | 321 List<jsAst.Property> statics = new List<jsAst.Property>(); |
| 314 ClassBuilder staticsBuilder = new ClassBuilder(); | 322 ClassBuilder staticsBuilder = new ClassBuilder(); |
| 315 if (emitFields(classElement, staticsBuilder, null, emitStatics: true)) { | 323 if (emitFields(classElement, staticsBuilder, null, emitStatics: true)) { |
| 316 statics.add(staticsBuilder.toObjectInitializer().properties.single); | 324 statics.add(staticsBuilder.properties.single); |
| 317 } | 325 } |
| 318 | 326 |
| 319 Map<String, ClassBuilder> classPropertyLists = | 327 Map<String, ClassBuilder> classPropertyLists = |
| 320 task.elementDecriptors.remove(classElement); | 328 task.elementDecriptors.remove(classElement); |
| 321 if (classPropertyLists != null) { | 329 if (classPropertyLists != null) { |
| 322 for (ClassBuilder classProperties in classPropertyLists.values) { | 330 for (ClassBuilder classProperties in classPropertyLists.values) { |
| 323 // TODO(ahe): What about deferred? | 331 // TODO(ahe): What about deferred? |
| 324 if (classProperties != null) { | 332 if (classProperties != null) { |
| 325 statics.addAll(classProperties.properties); | 333 statics.addAll(classProperties.properties); |
| 326 } | 334 } |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 computeTypeVariable = | 600 computeTypeVariable = |
| 593 js(r'this.$builtinTypeInfo && this.$builtinTypeInfo[#]', index); | 601 js(r'this.$builtinTypeInfo && this.$builtinTypeInfo[#]', index); |
| 594 } | 602 } |
| 595 jsAst.Expression convertRtiToRuntimeType = | 603 jsAst.Expression convertRtiToRuntimeType = |
| 596 namer.elementAccess(compiler.findHelper('convertRtiToRuntimeType')); | 604 namer.elementAccess(compiler.findHelper('convertRtiToRuntimeType')); |
| 597 builder.addProperty( | 605 builder.addProperty( |
| 598 name, js.fun( | 606 name, js.fun( |
| 599 [], [js.return_(convertRtiToRuntimeType(computeTypeVariable))])); | 607 [], [js.return_(convertRtiToRuntimeType(computeTypeVariable))])); |
| 600 } | 608 } |
| 601 } | 609 } |
| OLD | NEW |