OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library kernel.transformations.reify.transformation.remove_generics; |
| 6 |
| 7 import 'package:kernel/ast.dart'; |
| 8 import 'transformer.dart'; |
| 9 |
| 10 class Erasure extends Transformer with DartTypeVisitor<DartType> { |
| 11 final ReifyVisitor reifyVisitor; |
| 12 |
| 13 Erasure(this.reifyVisitor); |
| 14 |
| 15 bool removeTypeParameters(Class cls) { |
| 16 return reifyVisitor.needsTypeInformation(cls); |
| 17 } |
| 18 |
| 19 TreeNode removeTypeArgumentsOfConstructorCall(ConstructorInvocation node) { |
| 20 Class cls = node.target.parent; |
| 21 if (removeTypeParameters(cls)) { |
| 22 node.arguments.types.clear(); |
| 23 Constructor target = node.target; |
| 24 target.enclosingClass.typeParameters.clear(); |
| 25 } |
| 26 return node; |
| 27 } |
| 28 |
| 29 TreeNode removeTypeArgumentsOfStaticCall(StaticInvocation node) { |
| 30 Class cls = node.target.parent; |
| 31 if (removeTypeParameters(cls)) { |
| 32 node.arguments.types.clear(); |
| 33 Procedure target = node.target; |
| 34 target.function.typeParameters.clear(); |
| 35 } |
| 36 return node; |
| 37 } |
| 38 |
| 39 @override |
| 40 defaultDartType(DartType type) => type; |
| 41 |
| 42 @override |
| 43 InterfaceType visitInterfaceType(InterfaceType type) { |
| 44 if (removeTypeParameters(type.classNode)) { |
| 45 return new InterfaceType(type.classNode, const <DartType>[]); |
| 46 } |
| 47 return type; |
| 48 } |
| 49 |
| 50 @override |
| 51 Supertype visitSupertype(Supertype type) { |
| 52 if (removeTypeParameters(type.classNode)) { |
| 53 return new Supertype(type.classNode, const <DartType>[]); |
| 54 } |
| 55 return type; |
| 56 } |
| 57 |
| 58 @override |
| 59 FunctionType visitFunctionType(FunctionType type) { |
| 60 bool partHasChanged = false; |
| 61 |
| 62 DartType translate(DartType type) { |
| 63 DartType newType = type.accept(this); |
| 64 if (newType != type) { |
| 65 partHasChanged = true; |
| 66 return newType; |
| 67 } else { |
| 68 return type; |
| 69 } |
| 70 } |
| 71 |
| 72 DartType returnType = translate(type.returnType); |
| 73 List<DartType> positionalTypes = |
| 74 type.positionalParameters.map(translate).toList(); |
| 75 List<NamedType> namedParameters = new List<NamedType>(); |
| 76 for (NamedType param in type.namedParameters) { |
| 77 namedParameters.add(new NamedType(param.name, translate(param.type))); |
| 78 } |
| 79 if (partHasChanged) { |
| 80 return new FunctionType(positionalTypes, returnType, |
| 81 namedParameters: namedParameters, |
| 82 requiredParameterCount: type.requiredParameterCount); |
| 83 } else { |
| 84 return type; |
| 85 } |
| 86 } |
| 87 |
| 88 @override |
| 89 DynamicType visitTypeParameterType(_) => const DynamicType(); |
| 90 |
| 91 @override |
| 92 DartType visitDartType(DartType type) { |
| 93 return type.accept(this); |
| 94 } |
| 95 |
| 96 @override |
| 97 StaticInvocation visitStaticInvocation(StaticInvocation node) { |
| 98 node.transformChildren(this); |
| 99 if (node.target.kind == ProcedureKind.Factory) { |
| 100 node = removeTypeArgumentsOfStaticCall(node); |
| 101 } |
| 102 return node; |
| 103 } |
| 104 |
| 105 @override |
| 106 ConstructorInvocation visitConstructorInvocation(ConstructorInvocation node) { |
| 107 node.transformChildren(this); |
| 108 return removeTypeArgumentsOfConstructorCall(node); |
| 109 } |
| 110 |
| 111 @override |
| 112 Class visitClass(Class node) { |
| 113 node.transformChildren(this); |
| 114 if (removeTypeParameters(node)) { |
| 115 node.typeParameters.clear(); |
| 116 } |
| 117 return node; |
| 118 } |
| 119 } |
OLD | NEW |