| 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 js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Handles construction of TypeVariable constants needed at runtime. | 8 * Handles construction of TypeVariable constants needed at runtime. |
| 9 */ | 9 */ |
| 10 class TypeVariableHandler { | 10 class TypeVariableHandler { |
| 11 final Compiler _compiler; | 11 final Compiler _compiler; |
| 12 FunctionElement _typeVariableConstructor; | 12 ConstructorElement _typeVariableConstructor; |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Set to 'true' on first encounter of a class with type variables. | 15 * Set to 'true' on first encounter of a class with type variables. |
| 16 */ | 16 */ |
| 17 bool _seenClassesWithTypeVariables = false; | 17 bool _seenClassesWithTypeVariables = false; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Maps a class element to a list with indices that point to type variables | 20 * Maps a class element to a list with indices that point to type variables |
| 21 * constants for each of the class' type variables. | 21 * constants for each of the class' type variables. |
| 22 */ | 22 */ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 void processTypeVariablesOf(ClassElement cls) { | 70 void processTypeVariablesOf(ClassElement cls) { |
| 71 // Do not process classes twice. | 71 // Do not process classes twice. |
| 72 if (_typeVariables.containsKey(cls)) return; | 72 if (_typeVariables.containsKey(cls)) return; |
| 73 | 73 |
| 74 InterfaceType typeVariableType = _typeVariableClass.thisType; | 74 InterfaceType typeVariableType = _typeVariableClass.thisType; |
| 75 List<jsAst.Expression> constants = <jsAst.Expression>[]; | 75 List<jsAst.Expression> constants = <jsAst.Expression>[]; |
| 76 | 76 |
| 77 for (TypeVariableType currentTypeVariable in cls.typeVariables) { | 77 for (TypeVariableType currentTypeVariable in cls.typeVariables) { |
| 78 TypeVariableElement typeVariableElement = currentTypeVariable.element; | 78 TypeVariableElement typeVariableElement = currentTypeVariable.element; |
| 79 | 79 |
| 80 AstConstant name = new AstConstant( | |
| 81 typeVariableElement, | |
| 82 typeVariableElement.node, | |
| 83 new StringConstantExpression(currentTypeVariable.name), | |
| 84 _backend.constantSystem | |
| 85 .createString(new DartString.literal(currentTypeVariable.name))); | |
| 86 jsAst.Expression boundIndex = | 80 jsAst.Expression boundIndex = |
| 87 _metadataCollector.reifyType(typeVariableElement.bound); | 81 _metadataCollector.reifyType(typeVariableElement.bound); |
| 88 ConstantValue boundValue = new SyntheticConstantValue( | 82 ConstantValue boundValue = new SyntheticConstantValue( |
| 89 SyntheticConstantKind.TYPEVARIABLE_REFERENCE, boundIndex); | 83 SyntheticConstantKind.TYPEVARIABLE_REFERENCE, boundIndex); |
| 90 ConstantExpression boundExpression = | 84 ConstantExpression boundExpression = |
| 91 new SyntheticConstantExpression(boundValue); | 85 new SyntheticConstantExpression(boundValue); |
| 92 AstConstant bound = new AstConstant(typeVariableElement, | 86 ConstantExpression constant = new ConstructedConstantExpression( |
| 93 typeVariableElement.node, boundExpression, boundValue); | 87 _typeVariableConstructor.enclosingClass.thisType, |
| 94 AstConstant type = new AstConstant( | 88 _typeVariableConstructor, |
| 95 typeVariableElement, | 89 const CallStructure.unnamed(3), [ |
| 96 typeVariableElement.node, | 90 new TypeConstantExpression(cls.rawType), |
| 97 new TypeConstantExpression(cls.rawType), | 91 new StringConstantExpression(currentTypeVariable.name), |
| 98 _backend.constantSystem.createType(_backend.compiler, cls.rawType)); | 92 new SyntheticConstantExpression(boundValue) |
| 99 List<AstConstant> arguments = [type, name, bound]; | 93 ]); |
| 100 | 94 |
| 101 // TODO(johnniwinther): Support a less front-end specific creation of | 95 _backend.constants.evaluate(constant); |
| 102 // constructed constants. | 96 ConstantValue value = _backend.constants.getConstantValue(constant); |
| 103 AstConstant constant = | |
| 104 CompileTimeConstantEvaluator.makeConstructedConstant( | |
| 105 _compiler, | |
| 106 _backend.constants, | |
| 107 typeVariableElement, | |
| 108 typeVariableElement.node, | |
| 109 typeVariableType, | |
| 110 _typeVariableConstructor, | |
| 111 typeVariableType, | |
| 112 _typeVariableConstructor, | |
| 113 const CallStructure.unnamed(3), | |
| 114 arguments, | |
| 115 arguments); | |
| 116 ConstantValue value = constant.value; | |
| 117 _backend.registerCompileTimeConstant(value, _compiler.globalDependencies); | 97 _backend.registerCompileTimeConstant(value, _compiler.globalDependencies); |
| 118 _backend.addCompileTimeConstantForEmission(value); | 98 _backend.addCompileTimeConstantForEmission(value); |
| 119 _backend.constants.addCompileTimeConstantForEmission(value); | 99 _backend.constants.addCompileTimeConstantForEmission(value); |
| 120 constants | 100 constants |
| 121 .add(_reifyTypeVariableConstant(value, currentTypeVariable.element)); | 101 .add(_reifyTypeVariableConstant(value, currentTypeVariable.element)); |
| 122 } | 102 } |
| 123 _typeVariables[cls] = constants; | 103 _typeVariables[cls] = constants; |
| 124 } | 104 } |
| 125 | 105 |
| 126 /** | 106 /** |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 144 } |
| 165 | 145 |
| 166 List<jsAst.Expression> typeVariablesOf(ClassElement classElement) { | 146 List<jsAst.Expression> typeVariablesOf(ClassElement classElement) { |
| 167 List<jsAst.Expression> result = _typeVariables[classElement]; | 147 List<jsAst.Expression> result = _typeVariables[classElement]; |
| 168 if (result == null) { | 148 if (result == null) { |
| 169 result = const <jsAst.Expression>[]; | 149 result = const <jsAst.Expression>[]; |
| 170 } | 150 } |
| 171 return result; | 151 return result; |
| 172 } | 152 } |
| 173 } | 153 } |
| OLD | NEW |