Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 7 /** |
| 8 * Generates the code for all used classes in the program. Static fields (even | 8 * Generates the code for all used classes in the program. Static fields (even |
| 9 * in classes) are ignored, since they can be treated as non-class elements. | 9 * in classes) are ignored, since they can be treated as non-class elements. |
| 10 * | 10 * |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 /// type, and return types. | 40 /// type, and return types. |
| 41 final List<String> globalMetadata = []; | 41 final List<String> globalMetadata = []; |
| 42 | 42 |
| 43 /// A map used to canonicalize the entries of globalMetadata. | 43 /// A map used to canonicalize the entries of globalMetadata. |
| 44 final Map<String, int> globalMetadataMap = <String, int>{}; | 44 final Map<String, int> globalMetadataMap = <String, int>{}; |
| 45 | 45 |
| 46 // TODO(ngeoffray): remove this field. | 46 // TODO(ngeoffray): remove this field. |
| 47 Set<ClassElement> instantiatedClasses; | 47 Set<ClassElement> instantiatedClasses; |
| 48 | 48 |
| 49 JavaScriptBackend get backend => compiler.backend; | 49 JavaScriptBackend get backend => compiler.backend; |
| 50 TypeVariableConstantHandler get typeVarHandler => backend.typeVarHandler; | |
| 50 | 51 |
| 51 String get _ => space; | 52 String get _ => space; |
| 52 String get space => compiler.enableMinification ? "" : " "; | 53 String get space => compiler.enableMinification ? "" : " "; |
| 53 String get n => compiler.enableMinification ? "" : "\n"; | 54 String get n => compiler.enableMinification ? "" : "\n"; |
| 54 String get N => compiler.enableMinification ? "\n" : ";\n"; | 55 String get N => compiler.enableMinification ? "\n" : ";\n"; |
| 55 | 56 |
| 56 /** | 57 /** |
| 57 * Raw ClassElement symbols occuring in is-checks and type assertions. If the | 58 * Raw ClassElement symbols occuring in is-checks and type assertions. If the |
| 58 * program contains parameterized checks `x is Set<int>` and | 59 * program contains parameterized checks `x is Set<int>` and |
| 59 * `x is Set<String>` then the ClassElement `Set` will occur once in | 60 * `x is Set<String>` then the ClassElement `Set` will occur once in |
| (...skipping 1647 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1707 ClassBuilder builder, | 1708 ClassBuilder builder, |
| 1708 CodeBuffer buffer) { | 1709 CodeBuffer buffer) { |
| 1709 var metadata = buildMetadataFunction(classElement); | 1710 var metadata = buildMetadataFunction(classElement); |
| 1710 if (metadata != null) { | 1711 if (metadata != null) { |
| 1711 builder.addProperty("@", metadata); | 1712 builder.addProperty("@", metadata); |
| 1712 } | 1713 } |
| 1713 | 1714 |
| 1714 if (backend.isNeededForReflection(classElement)) { | 1715 if (backend.isNeededForReflection(classElement)) { |
| 1715 Link typeVars = classElement.typeVariables; | 1716 Link typeVars = classElement.typeVariables; |
| 1716 List properties = []; | 1717 List properties = []; |
| 1717 for (TypeVariableType typeVar in typeVars) { | 1718 if (typeVarHandler.typeVars[classElement] != null) |
|
karlklose
2013/10/14 10:54:43
Use a {} block even for one statement unless it is
ahe
2013/10/14 13:07:07
Please keep abstractions in place.
typeVarHandler
zarah
2013/10/15 14:20:02
Done.
zarah
2013/10/15 14:20:02
Done.
| |
| 1718 properties.add(js.string(typeVar.name.slowToString())); | 1719 properties.addAll(typeVarHandler.typeVars[classElement] |
| 1719 properties.add(js.toExpression(reifyType(typeVar.element.bound))); | 1720 .map((s) => js.toExpression(s))); |
|
ahe
2013/10/14 13:07:07
This should probably be:
properties = typeVarHand
zarah
2013/10/15 14:20:02
Done.
| |
| 1720 } | |
| 1721 | 1721 |
| 1722 ClassElement superclass = classElement.superclass; | 1722 ClassElement superclass = classElement.superclass; |
| 1723 bool hasSuper = superclass != null; | 1723 bool hasSuper = superclass != null; |
| 1724 if ((!properties.isEmpty && !hasSuper) || | 1724 if ((!properties.isEmpty && !hasSuper) || |
| 1725 (hasSuper && superclass.typeVariables != typeVars)) { | 1725 (hasSuper && superclass.typeVariables != typeVars)) { |
| 1726 builder.addProperty('<>', new jsAst.ArrayInitializer.from(properties)); | 1726 builder.addProperty('<>', new jsAst.ArrayInitializer.from(properties)); |
| 1727 } | 1727 } |
| 1728 } | 1728 } |
| 1729 List<CodeBuffer> classBuffers = elementBuffers[classElement]; | 1729 List<CodeBuffer> classBuffers = elementBuffers[classElement]; |
| 1730 if (classBuffers == null) { | 1730 if (classBuffers == null) { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1756 } | 1756 } |
| 1757 | 1757 |
| 1758 // TODO(ahe): This method (generateClass) should return a jsAst.Expression. | 1758 // TODO(ahe): This method (generateClass) should return a jsAst.Expression. |
| 1759 if (!buffer.isEmpty) { | 1759 if (!buffer.isEmpty) { |
| 1760 buffer.write(',$n$n'); | 1760 buffer.write(',$n$n'); |
| 1761 } | 1761 } |
| 1762 buffer.write('$className:$_'); | 1762 buffer.write('$className:$_'); |
| 1763 buffer.write(jsAst.prettyPrint(builder.toObjectInitializer(), compiler)); | 1763 buffer.write(jsAst.prettyPrint(builder.toObjectInitializer(), compiler)); |
| 1764 String reflectionName = getReflectionName(classElement, className); | 1764 String reflectionName = getReflectionName(classElement, className); |
| 1765 if (reflectionName != null) { | 1765 if (reflectionName != null) { |
| 1766 List<int> interfaces = <int>[]; | 1766 if (!backend.isNeededForReflection(classElement)) { |
| 1767 for (DartType interface in classElement.interfaces) { | 1767 buffer.write(',$n$n"+$reflectionName": 0'); |
| 1768 interfaces.add(reifyType(interface)); | 1768 } else { |
| 1769 List<int> interfaces = <int>[]; | |
| 1770 for (DartType interface in classElement.interfaces) { | |
| 1771 interfaces.add(reifyType(interface)); | |
| 1772 } | |
| 1773 buffer.write(',$n$n"+$reflectionName": $interfaces'); | |
| 1769 } | 1774 } |
| 1770 buffer.write(',$n$n"+$reflectionName": $interfaces'); | |
| 1771 } | 1775 } |
| 1772 } | 1776 } |
| 1773 | 1777 |
| 1774 /// If this is true then we can generate the noSuchMethod handlers at startup | 1778 /// If this is true then we can generate the noSuchMethod handlers at startup |
| 1775 /// time, instead of them being emitted as part of the Object class. | 1779 /// time, instead of them being emitted as part of the Object class. |
| 1776 bool get generateTrivialNsmHandlers => true; | 1780 bool get generateTrivialNsmHandlers => true; |
| 1777 | 1781 |
| 1778 int _selectorRank(Selector selector) { | 1782 int _selectorRank(Selector selector) { |
| 1779 int arity = selector.argumentCount * 3; | 1783 int arity = selector.argumentCount * 3; |
| 1780 if (selector.isGetter()) return arity + 2; | 1784 if (selector.isGetter()) return arity + 2; |
| (...skipping 1262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3043 if (value == null) { | 3047 if (value == null) { |
| 3044 compiler.reportInternalError( | 3048 compiler.reportInternalError( |
| 3045 annotation, 'Internal error: value is null'); | 3049 annotation, 'Internal error: value is null'); |
| 3046 return -1; | 3050 return -1; |
| 3047 } | 3051 } |
| 3048 return addGlobalMetadata( | 3052 return addGlobalMetadata( |
| 3049 jsAst.prettyPrint(constantReference(value), compiler).getText()); | 3053 jsAst.prettyPrint(constantReference(value), compiler).getText()); |
| 3050 } | 3054 } |
| 3051 | 3055 |
| 3052 int reifyType(DartType type) { | 3056 int reifyType(DartType type) { |
| 3053 // TODO(ahe): Handle type variables correctly instead of using "#". | 3057 jsAst.Expression representation = |
| 3054 String representation = backend.rti.getTypeRepresentation(type, (_) {}); | 3058 backend.rti.getTypeRepresentation(type, (variable) { |
| 3055 return addGlobalMetadata(representation.replaceAll('#', 'null')); | 3059 return js.toExpression(typeVarHandler.reifyTypeVar(variable)); |
| 3060 }); | |
|
ahe
2013/10/14 13:07:07
Weird indentation.
zarah
2013/10/15 14:20:02
Done.
| |
| 3061 | |
| 3062 return addGlobalMetadata( | |
| 3063 jsAst.prettyPrint(representation, compiler).getText()); | |
| 3056 } | 3064 } |
| 3057 | 3065 |
| 3058 int reifyName(SourceString name) { | 3066 int reifyName(SourceString name) { |
| 3059 return addGlobalMetadata('"${name.slowToString()}"'); | 3067 return addGlobalMetadata('"${name.slowToString()}"'); |
| 3060 } | 3068 } |
| 3061 | 3069 |
| 3062 int addGlobalMetadata(String string) { | 3070 int addGlobalMetadata(String string) { |
| 3063 return globalMetadataMap.putIfAbsent(string, () { | 3071 return globalMetadataMap.putIfAbsent(string, () { |
| 3064 globalMetadata.add(string); | 3072 globalMetadata.add(string); |
| 3065 return globalMetadata.length - 1; | 3073 return globalMetadata.length - 1; |
| (...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3598 } | 3606 } |
| 3599 | 3607 |
| 3600 bool isDeferred(Element element) { | 3608 bool isDeferred(Element element) { |
| 3601 return compiler.deferredLoadTask.isDeferred(element); | 3609 return compiler.deferredLoadTask.isDeferred(element); |
| 3602 } | 3610 } |
| 3603 | 3611 |
| 3604 bool get areAnyElementsDeferred { | 3612 bool get areAnyElementsDeferred { |
| 3605 return compiler.deferredLoadTask.areAnyElementsDeferred; | 3613 return compiler.deferredLoadTask.areAnyElementsDeferred; |
| 3606 } | 3614 } |
| 3607 } | 3615 } |
| OLD | NEW |