Chromium Code Reviews| Index: sdk/lib/_internal/compiler/implementation/js_backend/typeVariableConstantHandler.dart |
| diff --git a/sdk/lib/_internal/compiler/implementation/js_backend/typeVariableConstantHandler.dart b/sdk/lib/_internal/compiler/implementation/js_backend/typeVariableConstantHandler.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5684a92bdcf8249b813dfbbcec49cf02569985df |
| --- /dev/null |
| +++ b/sdk/lib/_internal/compiler/implementation/js_backend/typeVariableConstantHandler.dart |
| @@ -0,0 +1,98 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
|
ahe
2013/10/14 13:07:07
The file naming convention is:
foo_bar_baz.dart,
zarah
2013/10/15 14:20:02
Done.
|
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of js_backend; |
| + |
| +/** |
| + * Handles construction of TypeVariable constants needed on runtime. |
|
ahe
2013/10/14 13:07:07
on -> at.
zarah
2013/10/15 14:20:02
Done.
|
| + */ |
| +class TypeVariableConstantHandler { |
|
ahe
2013/10/14 13:07:07
The name "TypeVariableConstantHandler" is suboptim
zarah
2013/10/15 14:20:02
Done.
|
| + |
|
karlklose
2013/10/14 10:54:43
Remove line.
zarah
2013/10/15 14:20:02
Done.
|
| + JavaScriptBackend backend; |
| + ClassElement typeVarClass; |
|
ahe
2013/10/14 13:07:07
Try to avoid abbreviating typeVariable as typeVar.
zarah
2013/10/15 14:20:02
Done.
|
| + FunctionElement typeVarConstructor; |
| + Compiler get compiler => backend.compiler; |
|
ahe
2013/10/14 13:07:07
Please don't mix getters with fields. Follow this
zarah
2013/10/15 14:20:02
Done.
|
| + CodeEmitterTask get emitter => backend.emitter; |
| + CompileTimeConstantEvaluator evaluator; |
| + |
|
karlklose
2013/10/14 10:54:43
Perhaps add a bit of documentation for what classe
zarah
2013/10/15 14:20:02
Done.
|
| + List<ClassElement> typeVarClasses = new List<ClassElement>(); |
| + Map<ClassElement, List<int>> typeVars = new Map(); |
|
karlklose
2013/10/14 10:54:43
Add type arguments in new expression - otherwise t
zarah
2013/10/15 14:20:02
Done.
|
| + Map<TypeVariableType, int> typeVarConstants = |
| + new Map<TypeVariableType, int>(); |
| + |
| + TypeVariableConstantHandler(this.backend); |
| + |
| + void initializeHelperClass() { |
| + typeVarClass = |
|
karlklose
2013/10/14 10:54:43
Indentation.
zarah
2013/10/15 14:20:02
Removed from here.
|
| + backend.compiler.findHelper(const SourceString('TypeVariable')); |
| + } |
| + |
| + void registerClassWithTypeVars(ClassElement cls) { |
| + typeVarClasses.add(cls); |
| + } |
| + |
| + void onResolutionQueueEmpty(Enqueuer enqueuer) { |
| + if (typeVarConstructor == null) { |
| + typeVarConstructor = typeVarClass.constructors.head; |
|
ahe
2013/10/14 13:07:07
report an internal error if typeVarClass.construct
zarah
2013/10/15 14:20:02
Done.
|
| + backend.enqueueClass(enqueuer, typeVarClass, compiler.globalDependencies); |
| + backend.enqueueInResolution( |
| + typeVarConstructor, compiler.globalDependencies); |
| + } |
| + } |
| + |
| + void onCodegenQueueEmpty() { |
| + evaluator = new CompileTimeConstantEvaluator( |
| + compiler.constantHandler, compiler.globalDependencies, compiler); |
| + |
| + for (ClassElement cls in typeVarClasses) { |
| + if (!backend.isNeededForReflection(cls)) continue; |
| + |
| + TypeVariableType currentTypeVar; |
| + List<Constant> createArguments(_) { |
|
ahe
2013/10/14 13:07:07
What is the argument to createArguments, and why d
zarah
2013/10/15 14:20:02
Done.
|
| + Constant name = backend.constantSystem.createString( |
|
karlklose
2013/10/14 10:54:43
Remove one space.
zarah
2013/10/15 14:20:02
Done.
|
| + new DartString.literal(currentTypeVar.name.slowToString()), null); |
| + Constant bound = backend.constantSystem.createInt( |
| + emitter.reifyType(currentTypeVar.element.bound)); |
| + Constant type = evaluator.makeTypeConstant(cls); |
| + return [type, name, bound]; |
| + } |
| + |
| + InterfaceType typeVarType = typeVarClass.computeType(compiler); |
| + List<int> constants = []; |
|
karlklose
2013/10/14 10:54:43
'[]' -> '<int>[]'
ahe
2013/10/14 13:07:07
I actually think it would be better to use a Link
zarah
2013/10/15 14:20:02
Done.
|
| + for (TypeVariableType typeVar in cls.typeVariables) { |
| + currentTypeVar = typeVar; |
|
ahe
2013/10/14 13:07:07
You should be able to write:
for (currentTypeVar
zarah
2013/10/15 14:20:02
Done.
|
| + Constant c = evaluator.makeConstructedConstant( |
| + typeVar.element, typeVarType, typeVarConstructor, createArguments); |
| + backend.registerCompileTimeConstant(c, compiler.globalDependencies); |
| + compiler.constantHandler.addCompileTimeConstantForEmission(c); |
| + constants.add(reifyTypeVarConstant(c, typeVar)); |
| + } |
| + typeVars[cls] = constants; |
| + } |
| + typeVarClasses.clear(); |
| + } |
| + |
| + int reifyTypeVarConstant(Constant c, TypeVariableType variable) { |
| + String name = |
| + jsAst.prettyPrint(emitter.constantReference(c), compiler).getText(); |
| + int index; |
| + if (typeVarConstants.containsKey(variable)) { |
| + index = typeVarConstants[variable]; |
| + emitter.globalMetadata[index] = name; |
| + } else { |
| + index = emitter.addGlobalMetadata(name); |
| + typeVarConstants[variable] = index; |
| + } |
| + return index; |
| + } |
| + |
| + int reifyTypeVar(TypeVariableType variable) { |
| + if (typeVarConstants.containsKey(variable)) { |
| + return typeVarConstants[variable]; |
| + } |
| + |
| + emitter.globalMetadata.add('DUMMY'); |
|
karlklose
2013/10/14 10:54:43
Is this debug code? Shouldn't you assert that the
ahe
2013/10/14 13:07:07
Please document the relationship between reifyType
zarah
2013/10/15 14:20:02
Done.
|
| + return typeVarConstants[variable] = emitter.globalMetadata.length - 1; |
| + } |
| +} |