| 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 ClassStubGenerator get _stubGenerator => | 9 ClassStubGenerator get _stubGenerator => |
| 10 new ClassStubGenerator(compiler, namer, backend); | 10 new ClassStubGenerator(compiler, namer, backend); |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * Documentation wanted -- johnniwinther | 13 * Documentation wanted -- johnniwinther |
| 14 * |
| 15 * Invariant: [classElement] must be a declaration element. |
| 14 */ | 16 */ |
| 15 void emitClass(Class cls, | 17 void generateClass(ClassElement classElement, |
| 16 ClassBuilder enclosingBuilder, | 18 ClassBuilder properties, |
| 17 Map<String, jsAst.Expression> additionalProperties) { | 19 Map<String, jsAst.Expression> additionalProperties) { |
| 18 ClassElement classElement = cls.element; | |
| 19 final onlyForRti = | 20 final onlyForRti = |
| 20 emitter.typeTestRegistry.rtiNeededClasses.contains(classElement); | 21 emitter.typeTestRegistry.rtiNeededClasses.contains(classElement); |
| 21 | 22 |
| 22 assert(invariant(classElement, classElement.isDeclaration)); | 23 assert(invariant(classElement, classElement.isDeclaration)); |
| 23 assert(invariant(classElement, !classElement.isNative || onlyForRti)); | 24 assert(invariant(classElement, !classElement.isNative || onlyForRti)); |
| 24 | 25 |
| 25 emitter.needsClassSupport = true; | 26 emitter.needsClassSupport = true; |
| 26 String className = namer.getNameOfClass(classElement); | 27 String className = namer.getNameOfClass(classElement); |
| 27 | 28 |
| 28 ClassElement superclass = classElement.superclass; | 29 ClassElement superclass = classElement.superclass; |
| 29 String superName = ""; | 30 String superName = ""; |
| 30 if (superclass != null) { | 31 if (superclass != null) { |
| 31 superName = namer.getNameOfClass(superclass); | 32 superName = namer.getNameOfClass(superclass); |
| 32 } | 33 } |
| 33 | 34 |
| 34 if (cls.isMixinApplication) { | 35 if (classElement.isMixinApplication) { |
| 35 MixinApplication mixinApplication = cls; | 36 String mixinName = namer.getNameOfClass(computeMixinClass(classElement)); |
| 36 String mixinName = mixinApplication.mixinClass.name; | |
| 37 superName = '$superName+$mixinName'; | 37 superName = '$superName+$mixinName'; |
| 38 emitter.needsMixinSupport = true; | 38 emitter.needsMixinSupport = true; |
| 39 } | 39 } |
| 40 | 40 |
| 41 ClassBuilder builder = new ClassBuilder(classElement, namer); | 41 ClassBuilder builder = new ClassBuilder(classElement, namer); |
| 42 builder.superName = superName; | 42 builder.superName = superName; |
| 43 emitClassConstructor(classElement, builder, onlyForRti: onlyForRti); | 43 emitClassConstructor(classElement, builder, onlyForRti: onlyForRti); |
| 44 emitFields(classElement, builder, onlyForRti: onlyForRti); | 44 emitFields(classElement, builder, onlyForRti: onlyForRti); |
| 45 emitClassGettersSetters(classElement, builder, onlyForRti: onlyForRti); | 45 emitClassGettersSetters(classElement, builder, onlyForRti: onlyForRti); |
| 46 emitInstanceMembers(classElement, builder, onlyForRti: onlyForRti); | 46 emitInstanceMembers(classElement, builder, onlyForRti: onlyForRti); |
| 47 emitter.typeTestEmitter.emitIsTests(classElement, builder); | 47 emitter.typeTestEmitter.emitIsTests(classElement, builder); |
| 48 if (additionalProperties != null) { | 48 if (additionalProperties != null) { |
| 49 additionalProperties.forEach(builder.addProperty); | 49 additionalProperties.forEach(builder.addProperty); |
| 50 } | 50 } |
| 51 | 51 |
| 52 if (classElement == backend.closureClass) { | 52 if (classElement == backend.closureClass) { |
| 53 // We add a special getter here to allow for tearing off a closure from | 53 // We add a special getter here to allow for tearing off a closure from |
| 54 // itself. | 54 // itself. |
| 55 String name = namer.getMappedInstanceName(Compiler.CALL_OPERATOR_NAME); | 55 String name = namer.getMappedInstanceName(Compiler.CALL_OPERATOR_NAME); |
| 56 jsAst.Fun function = js('function() { return this; }'); | 56 jsAst.Fun function = js('function() { return this; }'); |
| 57 builder.addProperty(namer.getterNameFromAccessorName(name), function); | 57 builder.addProperty(namer.getterNameFromAccessorName(name), function); |
| 58 } | 58 } |
| 59 | 59 |
| 60 emitTypeVariableReaders(classElement, builder); | 60 emitTypeVariableReaders(classElement, builder); |
| 61 | 61 |
| 62 emitClassBuilderWithReflectionData( | 62 emitClassBuilderWithReflectionData( |
| 63 className, classElement, builder, enclosingBuilder); | 63 className, classElement, builder, properties); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void emitClassConstructor(ClassElement classElement, | 66 void emitClassConstructor(ClassElement classElement, |
| 67 ClassBuilder builder, | 67 ClassBuilder builder, |
| 68 {bool onlyForRti: false}) { | 68 {bool onlyForRti: false}) { |
| 69 List<String> fields = <String>[]; | 69 List<String> fields = <String>[]; |
| 70 if (!onlyForRti && !classElement.isNative) { | 70 if (!onlyForRti && !classElement.isNative) { |
| 71 visitFields(classElement, false, | 71 visitFields(classElement, false, |
| 72 (Element member, | 72 (Element member, |
| 73 String name, | 73 String name, |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 js(r'this.$builtinTypeInfo && this.$builtinTypeInfo[#]', index); | 594 js(r'this.$builtinTypeInfo && this.$builtinTypeInfo[#]', index); |
| 595 } | 595 } |
| 596 jsAst.Expression convertRtiToRuntimeType = emitter | 596 jsAst.Expression convertRtiToRuntimeType = emitter |
| 597 .staticFunctionAccess(backend.findHelper('convertRtiToRuntimeType')); | 597 .staticFunctionAccess(backend.findHelper('convertRtiToRuntimeType')); |
| 598 compiler.dumpInfoTask.registerElementAst(element, | 598 compiler.dumpInfoTask.registerElementAst(element, |
| 599 builder.addProperty(name, | 599 builder.addProperty(name, |
| 600 js('function () { return #(#) }', | 600 js('function () { return #(#) }', |
| 601 [convertRtiToRuntimeType, computeTypeVariable]))); | 601 [convertRtiToRuntimeType, computeTypeVariable]))); |
| 602 } | 602 } |
| 603 } | 603 } |
| OLD | NEW |