OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 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 | 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 library kernel.transformations.reify.transformation.remove_generics; | 5 library kernel.transformations.reify.transformation.remove_generics; |
6 | 6 |
7 import 'package:kernel/ast.dart'; | 7 import 'package:kernel/ast.dart'; |
8 import 'transformer.dart'; | 8 import 'transformer.dart'; |
9 | 9 |
10 class Erasure extends Transformer with DartTypeVisitor<DartType> { | 10 class Erasure extends Transformer with DartTypeVisitor<DartType> { |
11 final ReifyVisitor reifyVisitor; | 11 final ReifyVisitor reifyVisitor; |
12 | 12 |
13 Erasure(this.reifyVisitor); | 13 Erasure(this.reifyVisitor); |
14 | 14 |
15 bool removeTypeParameters(Class cls) { | 15 bool removeTypeParameters(Class cls) { |
16 return reifyVisitor.needsTypeInformation(cls); | 16 return reifyVisitor.needsTypeInformation(cls); |
17 } | 17 } |
18 | 18 |
19 TreeNode removeTypeArgumentsOfConstructorCall(ConstructorInvocation node) { | 19 TreeNode removeTypeArgumentsOfConstructorCall(ConstructorInvocation node) { |
20 Class cls = node.target.parent; | 20 Class cls = node.target.parent; |
21 if (removeTypeParameters(cls)) { | 21 if (removeTypeParameters(cls)) { |
22 node.arguments.types.clear(); | 22 node.arguments.types.clear(); |
23 Constructor target = node.target; | 23 Constructor target = node.target; |
24 target.enclosingClass.typeParameters.clear(); | 24 target.enclosingClass.typeParameters.clear(); |
25 } | 25 } |
26 return node; | 26 return node; |
27 } | 27 } |
28 | 28 |
29 TreeNode removeTypeArgumentsOfStaticCall(StaticInvocation node) { | 29 TreeNode removeTypeArgumentsOfStaticCall(StaticInvocation node) { |
30 Class cls = node.target.parent; | 30 if (node.target.parent is Class) { |
31 if (removeTypeParameters(cls)) { | 31 Class cls = node.target.parent; |
32 node.arguments.types.clear(); | 32 if (!removeTypeParameters(cls)) { |
33 Procedure target = node.target; | 33 return node; |
34 target.function.typeParameters.clear(); | 34 } |
| 35 } else { |
| 36 // If parent is a Library, then a global procedure is invoked, and it may |
| 37 // be a generic function, so we need to remove type arguments anyway. |
| 38 assert(node.target.parent is Library); |
35 } | 39 } |
| 40 node.arguments.types.clear(); |
| 41 Procedure target = node.target; |
| 42 target.function.typeParameters.clear(); |
36 return node; | 43 return node; |
37 } | 44 } |
38 | 45 |
| 46 TreeNode removeTypeArgumentOfMethodInvocation(MethodInvocation node) { |
| 47 node.arguments.types.clear(); |
| 48 return node; |
| 49 } |
| 50 |
39 @override | 51 @override |
40 defaultDartType(DartType type) => type; | 52 defaultDartType(DartType type) => type; |
41 | 53 |
42 @override | 54 @override |
43 InterfaceType visitInterfaceType(InterfaceType type) { | 55 InterfaceType visitInterfaceType(InterfaceType type) { |
44 if (removeTypeParameters(type.classNode)) { | 56 if (removeTypeParameters(type.classNode)) { |
45 return new InterfaceType(type.classNode, const <DartType>[]); | 57 return new InterfaceType(type.classNode, const <DartType>[]); |
46 } | 58 } |
47 return type; | 59 return type; |
48 } | 60 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 DynamicType visitTypeParameterType(_) => const DynamicType(); | 101 DynamicType visitTypeParameterType(_) => const DynamicType(); |
90 | 102 |
91 @override | 103 @override |
92 DartType visitDartType(DartType type) { | 104 DartType visitDartType(DartType type) { |
93 return type.accept(this); | 105 return type.accept(this); |
94 } | 106 } |
95 | 107 |
96 @override | 108 @override |
97 StaticInvocation visitStaticInvocation(StaticInvocation node) { | 109 StaticInvocation visitStaticInvocation(StaticInvocation node) { |
98 node.transformChildren(this); | 110 node.transformChildren(this); |
99 if (node.target.kind == ProcedureKind.Factory) { | 111 if (node.target.kind == ProcedureKind.Factory || |
| 112 node.target.kind == ProcedureKind.Method) { |
100 node = removeTypeArgumentsOfStaticCall(node); | 113 node = removeTypeArgumentsOfStaticCall(node); |
101 } | 114 } |
102 return node; | 115 return node; |
103 } | 116 } |
104 | 117 |
105 @override | 118 @override |
106 ConstructorInvocation visitConstructorInvocation(ConstructorInvocation node) { | 119 ConstructorInvocation visitConstructorInvocation(ConstructorInvocation node) { |
107 node.transformChildren(this); | 120 node.transformChildren(this); |
108 return removeTypeArgumentsOfConstructorCall(node); | 121 return removeTypeArgumentsOfConstructorCall(node); |
109 } | 122 } |
110 | 123 |
111 @override | 124 @override |
112 Class visitClass(Class node) { | 125 Class visitClass(Class node) { |
113 node.transformChildren(this); | 126 node.transformChildren(this); |
114 if (removeTypeParameters(node)) { | 127 if (removeTypeParameters(node)) { |
115 node.typeParameters.clear(); | 128 node.typeParameters.clear(); |
116 } | 129 } |
117 return node; | 130 return node; |
118 } | 131 } |
| 132 |
| 133 @override |
| 134 Expression visitMethodInvocation(MethodInvocation node) { |
| 135 node.transformChildren(this); |
| 136 return removeTypeArgumentOfMethodInvocation(node); |
| 137 } |
119 } | 138 } |
OLD | NEW |