| 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 * |
| 11 * Invariant: [classElement] must be a declaration element. | 11 * Invariant: [classElement] must be a declaration element. |
| 12 */ | 12 */ |
| 13 void generateClass(ClassElement classElement, | 13 void generateClass(ClassElement classElement, |
| 14 ClassBuilder properties, | 14 ClassBuilder properties, |
| 15 Map<String, jsAst.Expression> additionalProperties) { | 15 Map<String, jsAst.Expression> additionalProperties) { |
| 16 final onlyForRti = | 16 final onlyForRti = |
| 17 task.typeTestEmitter.rtiNeededClasses.contains(classElement); | 17 task.typeTestEmitter.rtiNeededClasses.contains(classElement); |
| 18 | 18 |
| 19 assert(invariant(classElement, classElement.isDeclaration)); | 19 assert(invariant(classElement, classElement.isDeclaration)); |
| 20 assert(invariant(classElement, !classElement.isNative() || onlyForRti)); | 20 assert(invariant(classElement, !classElement.isNative() || onlyForRti)); |
| 21 | 21 |
| 22 task.needsDefineClass = true; | 22 task.needsDefineClass = true; |
| 23 String className = namer.getNameOfClass(classElement); | 23 String className = namer.getNameOfClass(classElement); |
| 24 | 24 |
| 25 ClassElement superclass = classElement.superclass; | 25 ClassElement superclass = classElement.superclass; |
| 26 String superName = ""; | 26 String superName = ""; |
| 27 if (superclass != null) { | 27 if (superclass != null) { |
| 28 superName = namer.getNameOfClass(superclass); | 28 superName = namer.getNameOfClass(superclass); |
| 29 } | 29 } |
| 30 String runtimeName = | |
| 31 namer.getPrimitiveInterceptorRuntimeName(classElement); | |
| 32 | 30 |
| 33 if (classElement.isMixinApplication) { | 31 if (classElement.isMixinApplication) { |
| 34 String mixinName = namer.getNameOfClass(computeMixinClass(classElement)); | 32 String mixinName = namer.getNameOfClass(computeMixinClass(classElement)); |
| 35 superName = '$superName+$mixinName'; | 33 superName = '$superName+$mixinName'; |
| 36 task.needsMixinSupport = true; | 34 task.needsMixinSupport = true; |
| 37 } | 35 } |
| 38 | 36 |
| 39 ClassBuilder builder = new ClassBuilder(namer); | 37 ClassBuilder builder = new ClassBuilder(namer); |
| 40 emitClassConstructor(classElement, builder, runtimeName, | 38 emitClassConstructor(classElement, builder, onlyForRti: onlyForRti); |
| 41 onlyForRti: onlyForRti); | |
| 42 emitFields(classElement, builder, superName, onlyForRti: onlyForRti); | 39 emitFields(classElement, builder, superName, onlyForRti: onlyForRti); |
| 43 emitClassGettersSetters(classElement, builder, onlyForRti: onlyForRti); | 40 emitClassGettersSetters(classElement, builder, onlyForRti: onlyForRti); |
| 44 emitInstanceMembers(classElement, builder, onlyForRti: onlyForRti); | 41 emitInstanceMembers(classElement, builder, onlyForRti: onlyForRti); |
| 45 task.typeTestEmitter.emitIsTests(classElement, builder); | 42 task.typeTestEmitter.emitIsTests(classElement, builder); |
| 46 if (additionalProperties != null) { | 43 if (additionalProperties != null) { |
| 47 additionalProperties.forEach(builder.addProperty); | 44 additionalProperties.forEach(builder.addProperty); |
| 48 } | 45 } |
| 49 | 46 |
| 50 if (classElement == compiler.closureClass) { | 47 if (classElement == compiler.closureClass) { |
| 51 // We add a special getter here to allow for tearing off a closure from | 48 // We add a special getter here to allow for tearing off a closure from |
| 52 // itself. | 49 // itself. |
| 53 String name = namer.getMappedInstanceName(Compiler.CALL_OPERATOR_NAME); | 50 String name = namer.getMappedInstanceName(Compiler.CALL_OPERATOR_NAME); |
| 54 jsAst.Fun function = js('function() { return this; }'); | 51 jsAst.Fun function = js('function() { return this; }'); |
| 55 builder.addProperty(namer.getterNameFromAccessorName(name), function); | 52 builder.addProperty(namer.getterNameFromAccessorName(name), function); |
| 56 } | 53 } |
| 57 | 54 |
| 58 emitTypeVariableReaders(classElement, builder); | 55 emitTypeVariableReaders(classElement, builder); |
| 59 | 56 |
| 60 emitClassBuilderWithReflectionData( | 57 emitClassBuilderWithReflectionData( |
| 61 className, classElement, builder, properties); | 58 className, classElement, builder, properties); |
| 62 } | 59 } |
| 63 | 60 |
| 64 void emitClassConstructor(ClassElement classElement, | 61 void emitClassConstructor(ClassElement classElement, |
| 65 ClassBuilder builder, | 62 ClassBuilder builder, |
| 66 String runtimeName, | |
| 67 {bool onlyForRti: false}) { | 63 {bool onlyForRti: false}) { |
| 68 List<String> fields = <String>[]; | 64 List<String> fields = <String>[]; |
| 69 if (!onlyForRti && !classElement.isNative()) { | 65 if (!onlyForRti && !classElement.isNative()) { |
| 70 visitFields(classElement, false, | 66 visitFields(classElement, false, |
| 71 (Element member, | 67 (Element member, |
| 72 String name, | 68 String name, |
| 73 String accessorName, | 69 String accessorName, |
| 74 bool needsGetter, | 70 bool needsGetter, |
| 75 bool needsSetter, | 71 bool needsSetter, |
| 76 bool needsCheckedSetter) { | 72 bool needsCheckedSetter) { |
| 77 fields.add(name); | 73 fields.add(name); |
| 78 }); | 74 }); |
| 79 } | 75 } |
| 80 String constructorName = namer.getNameOfClass(classElement); | 76 String constructorName = namer.getNameOfClass(classElement); |
| 81 | 77 |
| 82 // TODO(sra): Implement placeholders in VariableDeclaration position: | 78 // TODO(sra): Implement placeholders in VariableDeclaration position: |
| 83 // task.precompiledFunction.add(js.statement('function #(#) { #; }', | 79 // task.precompiledFunction.add(js.statement('function #(#) { #; }', |
| 84 // [ constructorName, fields, | 80 // [ constructorName, fields, |
| 85 // fields.map( | 81 // fields.map( |
| 86 // (name) => js('this.# = #', [name, name]))])); | 82 // (name) => js('this.# = #', [name, name]))])); |
| 87 task.precompiledFunction.add( | 83 task.precompiledFunction.add( |
| 88 new jsAst.FunctionDeclaration( | 84 new jsAst.FunctionDeclaration( |
| 89 new jsAst.VariableDeclaration(constructorName), | 85 new jsAst.VariableDeclaration(constructorName), |
| 90 js('function(#) { #; }', | 86 js('function(#) { #; }', |
| 91 [fields, | 87 [fields, |
| 92 fields.map((name) => js('this.# = #', [name, name]))]))); | 88 fields.map((name) => js('this.# = #', [name, name]))]))); |
| 93 if (runtimeName == null) { | 89 // TODO(floitsch): do we actually need the name field? |
| 94 runtimeName = constructorName; | 90 // TODO(floitsch): these should all go through the namer. |
| 95 } | |
| 96 | 91 |
| 97 task.precompiledFunction.add( | 92 task.precompiledFunction.add( |
| 98 js.statement(r'''{ | 93 js.statement(r'''{ |
| 99 #.builtin$cls = #; | 94 #.builtin$cls = #; |
| 100 if (!"name" in #) | 95 if (!"name" in #) |
| 101 #.name = #; | 96 #.name = #; |
| 102 $desc=$collectedClasses.#; | 97 $desc=$collectedClasses.#; |
| 103 if ($desc instanceof Array) $desc = $desc[1]; | 98 if ($desc instanceof Array) $desc = $desc[1]; |
| 104 #.prototype = $desc; | 99 #.prototype = $desc; |
| 105 }''', | 100 }''', |
| 106 [ constructorName, js.string(runtimeName), | 101 [ constructorName, js.string(constructorName), |
| 107 constructorName, | 102 constructorName, |
| 108 constructorName, js.string(constructorName), | 103 constructorName, js.string(constructorName), |
| 109 constructorName, | 104 constructorName, |
| 110 constructorName | 105 constructorName |
| 111 ])); | 106 ])); |
| 112 | 107 |
| 113 task.precompiledConstructorNames.add(js('#', constructorName)); | 108 task.precompiledConstructorNames.add(js('#', constructorName)); |
| 114 } | 109 } |
| 115 | 110 |
| 116 /// Returns `true` if fields added. | 111 /// Returns `true` if fields added. |
| 117 bool emitFields(Element element, | 112 bool emitFields(Element element, |
| 118 ClassBuilder builder, | 113 ClassBuilder builder, |
| 119 String superName, | 114 String superName, |
| 120 { bool classIsNative: false, | 115 { bool classIsNative: false, |
| 121 bool emitStatics: false, | 116 bool emitStatics: false, |
| 122 bool onlyForRti: false }) { | 117 bool onlyForRti: false }) { |
| 123 assert(!emitStatics || !onlyForRti); | 118 assert(!emitStatics || !onlyForRti); |
| 124 if (element.isLibrary()) { | 119 if (element.isLibrary()) { |
| 125 assert(invariant(element, emitStatics)); | 120 assert(invariant(element, emitStatics)); |
| 126 } else if (!element.isClass()) { | 121 } else if (!element.isClass()) { |
| 127 throw new SpannableAssertionFailure( | 122 throw new SpannableAssertionFailure( |
| 128 element, 'Must be a ClassElement or a LibraryElement'); | 123 element, 'Must be a ClassElement or a LibraryElement'); |
| 129 } | 124 } |
| 130 if (emitStatics) { | 125 if (emitStatics) { |
| 131 assert(invariant(element, superName == null, message: superName)); | 126 assert(invariant(element, superName == null, message: superName)); |
| 132 } else { | 127 } else { |
| 133 assert(invariant(element, superName != null)); | 128 assert(invariant(element, superName != null)); |
| 134 String nativeName = | |
| 135 namer.getPrimitiveInterceptorRuntimeName(element); | |
| 136 if (nativeName != null) { | |
| 137 builder.nativeName = nativeName; | |
| 138 } | |
| 139 builder.superName = superName; | 129 builder.superName = superName; |
| 140 } | 130 } |
| 141 var fieldMetadata = []; | 131 var fieldMetadata = []; |
| 142 bool hasMetadata = false; | 132 bool hasMetadata = false; |
| 143 bool fieldsAdded = false; | 133 bool fieldsAdded = false; |
| 144 | 134 |
| 145 if (!onlyForRti) { | 135 if (!onlyForRti) { |
| 146 visitFields(element, emitStatics, | 136 visitFields(element, emitStatics, |
| 147 (VariableElement field, | 137 (VariableElement field, |
| 148 String name, | 138 String name, |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 computeTypeVariable = | 610 computeTypeVariable = |
| 621 js(r'this.$builtinTypeInfo && this.$builtinTypeInfo[#]', index); | 611 js(r'this.$builtinTypeInfo && this.$builtinTypeInfo[#]', index); |
| 622 } | 612 } |
| 623 jsAst.Expression convertRtiToRuntimeType = | 613 jsAst.Expression convertRtiToRuntimeType = |
| 624 namer.elementAccess(compiler.findHelper('convertRtiToRuntimeType')); | 614 namer.elementAccess(compiler.findHelper('convertRtiToRuntimeType')); |
| 625 builder.addProperty(name, | 615 builder.addProperty(name, |
| 626 js('function () { return #(#) }', | 616 js('function () { return #(#) }', |
| 627 [convertRtiToRuntimeType, computeTypeVariable])); | 617 [convertRtiToRuntimeType, computeTypeVariable])); |
| 628 } | 618 } |
| 629 } | 619 } |
| OLD | NEW |